Help us shape the future of Go! Take the 2026 Go Developer Survey

tendo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 13 Imported by: 7

README

tendo

CI Status codecov Go Report Card CodeQL Go Reference License Go Version Release

A composable tensor library for Go with GPU acceleration.

Tendo provides building blocks for neural network operations through a pipeline-based architecture, integrating with pipz for operation composition and capitan for observability.

Inference in Native Go

Load weights, run forward passes, get predictions — all in Go, with optional GPU acceleration:

backend := cuda.NewBackend(0) // or cpu.NewBackend()

// Load pretrained weights
weights := tendo.MustLoad("model.safetensors")

// Build the forward pass
linear := tendo.NewMatMul(backend, weights.Get("fc.weight"))
bias := tendo.NewAdd(backend, weights.Get("fc.bias"))
activation := tendo.NewGELU(backend)

// Run inference
output, err := pipz.NewSequence(ForwardID, linear, bias, activation).Process(ctx, input)

No Python runtime. No CGO for CPU operations. One binary, native error handling, production-ready.

Installation

go get github.com/zoobzio/tendo

Requires Go 1.24+. For CUDA support: NVIDIA CUDA Toolkit with cuBLAS

Quick Start

package main

import (
    "context"
    "fmt"

    "github.com/zoobzio/pipz"
    "github.com/zoobzio/tendo"
    "github.com/zoobzio/tendo/cpu"
)

var (
    LinearID = pipz.NewIdentity("linear", "Linear transformation")
)

func main() {
    ctx := context.Background()
    backend := cpu.NewBackend()

    // Create tensors
    input := tendo.MustFromSlice([]float32{1, 2, 3, 4, 5, 6}, 2, 3)
    weights := tendo.MustRandN(3, 4)
    bias := tendo.MustZeros(4)

    // Build operations
    matmul := tendo.NewMatMul(backend, weights)
    add := tendo.NewAdd(backend, bias)
    relu := tendo.NewReLU(backend)

    // Compose into a pipeline
    forward := pipz.NewSequence(LinearID, matmul, add, relu)

    // Execute
    output, err := forward.Process(ctx, input)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Input:  %v\n", input.Shape())  // [2, 3]
    fmt.Printf("Output: %v\n", output.Shape()) // [2, 4]
}

Why tendo?

  • Native Go: No CGO required for CPU operations. Clean interfaces, explicit error handling.
  • Composable: Operations are pipz.Chainable. Build computation graphs that compose and reuse.
  • Observable: Every operation emits capitan signals. Hook listeners for logging, profiling, debugging.
  • Device portable: Write operations once, run on CPU or CUDA. Transfer tensors with a single call.
  • Memory efficient: Pool allocations to reduce GC pressure and CUDA malloc overhead.

Capabilities

Feature Description Docs
Tensor Creation Zeros, ones, random, ranges, identity matrices Quickstart
Elementwise Ops Add, Sub, Mul, Div, Exp, Log, Sqrt, Pow, trig functions Operations
Matrix Ops MatMul, Transpose, batched multiplication Operations
Activations ReLU, Sigmoid, Tanh, GELU, SiLU, Softmax, Dropout Operations
Reductions Sum, Mean, Max, Min, Var, Std, ArgMax, ArgMin Operations
Shape Ops Reshape, Squeeze, Unsqueeze, Slice, Permute, Cat, Stack Operations
Convolution Conv2d, ConvTranspose2d with grouped convolution Operations
Pooling MaxPool2d, AvgPool2d, Adaptive variants Operations
Normalization BatchNorm, LayerNorm, RMSNorm, GroupNorm Operations
Device Support CPU and NVIDIA CUDA with seamless transfer Devices
Memory Pooling Allocation reuse to reduce GC and malloc overhead Memory
Data Types Float32, Float16, BFloat16 Data Types

From Training to Production

Train in PyTorch, deploy in Go. Tendo bridges the gap:

// Load exported weights
weights := tendo.MustLoad("transformer.safetensors")

// Build transformer blocks from tendo primitives
attention := NewMultiHeadAttention(backend, weights, "layer.0.attn")
ffn := NewFeedForward(backend, weights, "layer.0.ffn")
norm := tendo.NewLayerNorm(backend, weights.Get("layer.0.norm"))

// Compose into a model
layer := pipz.NewSequence(LayerID, attention, norm, ffn)

// Inference at scale — pure Go, GPU-accelerated
for batch := range batches {
    output, _ := layer.Process(ctx, batch)
    results <- output
}

Your inference service is a Go binary. Standard deployment, standard tooling, standard observability — with GPU performance where it matters.

Documentation

Contributing

Contributions are welcome. See CONTRIBUTING.md for development workflow and guidelines.

# Quick start
make help          # Show available commands
make install-tools # Install development tools
make check         # Run tests and lint

License

MIT

Documentation

Overview

Package tendo provides a composable tensor library for Go with GPU acceleration.

Overview

Tendo provides building blocks for neural network operations through a pipeline-based architecture. It integrates with pipz for operation composition and capitan for observability.

Core Types

The primary types are:

  • Tensor: Multi-dimensional array with shape, stride, and device information
  • Storage: Interface for memory backends (CPU, CUDA)
  • Device: Represents a compute device (CPU or GPU)
  • DType: Data type of tensor elements (Float32, Float16, BFloat16, Int64)
  • Pool: Memory pool for efficient allocation reuse

Tensor Creation

Create tensors using constructor functions:

t := tendo.FromSlice([]float32{1, 2, 3, 4, 5, 6}, 2, 3)
zeros := tendo.Zeros(3, 4)
ones := tendo.Ones(2, 2)
rand := tendo.RandN(10, 10)

Operations

Operations are organized into categories:

Device Management

Tensors can reside on CPU or CUDA devices:

tendo.SetDefaultDevice(tendo.CUDADevice(0))
t := tendo.ZerosOn(tendo.CUDADevice(0), 1024, 1024)
cpu := tendo.ToCPU(t)
gpu := tendo.ToCUDA(t, 0)

Memory Pooling

Use memory pools for efficient allocation reuse:

pool := tendo.NewPool()
storage := pool.AllocCPU(1000, tendo.Float32)
pool.FreeCPU(storage)

Pipeline Composition

Operations return pipz.Chainable for composable computation graphs:

forward := pipz.Chain(
    tendo.MatMul(weights),
    tendo.Add(bias),
    tendo.ReLU(),
)
result, err := forward.Run(ctx, input)

Backend Interface

The Backend interface defines the contract for compute backends. CPU backend is always available; CUDA backend requires GPU hardware.

See CoreBackend, ArithmeticBackend, MatrixBackend, NeuralBackend, ReduceBackend, and CompareBackend for capability-specific interfaces.

Package tendo provides a composable tensor library for Go with GPU acceleration.

Tendo builds on pipz for operation composition and capitan for observability, enabling construction of neural network inference pipelines with automatic event emission for logging, profiling, and debugging.

Index

Constants

View Source
const (
	ReductionNone = "none" // No reduction, return element-wise loss
	ReductionMean = "mean" // Return mean of losses
	ReductionSum  = "sum"  // Return sum of losses
)

Reduction modes for loss functions.

View Source
const (
	VariantTensor     capitan.Variant = "tendo.Tensor"
	VariantTensorList capitan.Variant = "tendo.TensorList"
	VariantShape      capitan.Variant = "tendo.Shape"
	VariantDevice     capitan.Variant = "tendo.Device"
	VariantIntPair    capitan.Variant = "tendo.IntPair"
	VariantUintptr    capitan.Variant = "tendo.Uintptr"
)

Custom variants for tendo types.

View Source
const VariantDType capitan.Variant = "tendo.DType"

VariantDType is the capitan variant for tensor data types.

Variables

View Source
var (
	// ErrShapeMismatch is returned when data length doesn't match shape.
	ErrShapeMismatch = errors.New("data length does not match shape")
	// ErrZeroStep is returned when step is zero in Arange.
	ErrZeroStep = errors.New("step cannot be zero")
)

Constructor errors.

View Source
var (
	OpCast     = capitan.NewSignal("tendo.op.cast", "Type cast")
	KeySrcType = capitan.NewKey[DType]("src_dtype", VariantDType)
	KeyDstType = capitan.NewKey[DType]("dst_dtype", VariantDType)
)

Observability keys for dtype operations.

View Source
var (
	// TensorCreated is emitted when a tensor is allocated.
	TensorCreated = capitan.NewSignal("tendo.tensor.created", "Tensor allocated")

	// TensorFreed is emitted when a tensor is deallocated.
	TensorFreed = capitan.NewSignal("tendo.tensor.freed", "Tensor deallocated")

	// TensorTransfer is emitted when a tensor is moved between devices.
	TensorTransfer = capitan.NewSignal("tendo.tensor.transfer", "Tensor moved between devices")
)

Lifecycle signals.

View Source
var (
	// Element-wise operations.
	OpAdd    = capitan.NewSignal("tendo.op.add", "Element-wise addition")
	OpSub    = capitan.NewSignal("tendo.op.sub", "Element-wise subtraction")
	OpMul    = capitan.NewSignal("tendo.op.mul", "Element-wise multiplication")
	OpDiv    = capitan.NewSignal("tendo.op.div", "Element-wise division")
	OpNeg    = capitan.NewSignal("tendo.op.neg", "Negation")
	OpAbs    = capitan.NewSignal("tendo.op.abs", "Absolute value")
	OpExp    = capitan.NewSignal("tendo.op.exp", "Exponential")
	OpLog    = capitan.NewSignal("tendo.op.log", "Natural logarithm")
	OpSqrt   = capitan.NewSignal("tendo.op.sqrt", "Square root")
	OpSquare = capitan.NewSignal("tendo.op.square", "Square")
	OpPow    = capitan.NewSignal("tendo.op.pow", "Power")
	OpSin    = capitan.NewSignal("tendo.op.sin", "Sine")
	OpCos    = capitan.NewSignal("tendo.op.cos", "Cosine")

	// Matrix operations.
	OpMatMul    = capitan.NewSignal("tendo.op.matmul", "Matrix multiplication")
	OpTranspose = capitan.NewSignal("tendo.op.transpose", "Transpose")

	// Shape operations.
	OpReshape   = capitan.NewSignal("tendo.op.reshape", "Reshape")
	OpSqueeze   = capitan.NewSignal("tendo.op.squeeze", "Squeeze dimension")
	OpUnsqueeze = capitan.NewSignal("tendo.op.unsqueeze", "Unsqueeze dimension")
	OpSlice     = capitan.NewSignal("tendo.op.slice", "Slice")
	OpExpand    = capitan.NewSignal("tendo.op.expand", "Expand/broadcast")
	OpPermute   = capitan.NewSignal("tendo.op.permute", "Dimension permutation")
	OpCat       = capitan.NewSignal("tendo.op.cat", "Concatenate")
	OpStack     = capitan.NewSignal("tendo.op.stack", "Stack")

	// Reduction operations.
	OpSum    = capitan.NewSignal("tendo.op.sum", "Sum reduction")
	OpMean   = capitan.NewSignal("tendo.op.mean", "Mean reduction")
	OpMax    = capitan.NewSignal("tendo.op.max", "Max reduction")
	OpMin    = capitan.NewSignal("tendo.op.min", "Min reduction")
	OpArgMax = capitan.NewSignal("tendo.op.argmax", "Argmax")
	OpArgMin = capitan.NewSignal("tendo.op.argmin", "Argmin")
	OpVar    = capitan.NewSignal("tendo.op.var", "Variance reduction")
	OpStd    = capitan.NewSignal("tendo.op.std", "Standard deviation reduction")
	OpProd   = capitan.NewSignal("tendo.op.prod", "Product reduction")

	// Activation operations.
	OpReLU       = capitan.NewSignal("tendo.op.relu", "ReLU activation")
	OpLeakyReLU  = capitan.NewSignal("tendo.op.leaky_relu", "Leaky ReLU activation")
	OpSigmoid    = capitan.NewSignal("tendo.op.sigmoid", "Sigmoid activation")
	OpTanh       = capitan.NewSignal("tendo.op.tanh", "Tanh activation")
	OpGELU       = capitan.NewSignal("tendo.op.gelu", "GELU activation")
	OpSiLU       = capitan.NewSignal("tendo.op.silu", "SiLU/Swish activation")
	OpSoftmax    = capitan.NewSignal("tendo.op.softmax", "Softmax")
	OpLogSoftmax = capitan.NewSignal("tendo.op.logsoftmax", "Log softmax")
	OpDropout    = capitan.NewSignal("tendo.op.dropout", "Dropout")

	// Pooling operations.
	OpMaxPool2d         = capitan.NewSignal("tendo.op.maxpool2d", "2D max pooling")
	OpAvgPool2d         = capitan.NewSignal("tendo.op.avgpool2d", "2D average pooling")
	OpAdaptiveAvgPool2d = capitan.NewSignal("tendo.op.adaptiveavgpool2d", "Adaptive 2D average pooling")
	OpAdaptiveMaxPool2d = capitan.NewSignal("tendo.op.adaptivemaxpool2d", "Adaptive 2D max pooling")

	// Normalization operations.
	OpBatchNorm    = capitan.NewSignal("tendo.op.batchnorm", "Batch normalization")
	OpLayerNorm    = capitan.NewSignal("tendo.op.layernorm", "Layer normalization")
	OpRMSNorm      = capitan.NewSignal("tendo.op.rmsnorm", "RMS normalization")
	OpGroupNorm    = capitan.NewSignal("tendo.op.groupnorm", "Group normalization")
	OpInstanceNorm = capitan.NewSignal("tendo.op.instancenorm", "Instance normalization")

	// Additional element-wise operations.
	OpClamp = capitan.NewSignal("tendo.op.clamp", "Clamp values")
	OpWhere = capitan.NewSignal("tendo.op.where", "Conditional select")
	OpSign  = capitan.NewSignal("tendo.op.sign", "Sign function")
	OpTril  = capitan.NewSignal("tendo.op.tril", "Lower triangular")

	// Convolution operations.
	OpConvTranspose2d = capitan.NewSignal("tendo.op.convtranspose2d", "2D transposed convolution")

	// Embedding operations.
	OpEmbedding = capitan.NewSignal("tendo.op.embedding", "Embedding lookup")

	// Loss functions.
	OpMSELoss          = capitan.NewSignal("tendo.op.mse_loss", "Mean squared error loss")
	OpL1Loss           = capitan.NewSignal("tendo.op.l1_loss", "L1 loss")
	OpCrossEntropyLoss = capitan.NewSignal("tendo.op.cross_entropy", "Cross entropy loss")
	OpNLLLoss          = capitan.NewSignal("tendo.op.nll_loss", "Negative log likelihood loss")
)

Operation signals - emitted during forward pass for observability.

View Source
var (
	// PoolAlloc is emitted when memory is allocated from the pool.
	PoolAlloc = capitan.NewSignal("tendo.pool.alloc", "Memory allocated from pool")

	// PoolFree is emitted when memory is returned to the pool.
	PoolFree = capitan.NewSignal("tendo.pool.free", "Memory returned to pool")
)

Memory signals.

View Source
var (
	// KeyInput is the primary input tensor for unary operations.
	KeyInput = capitan.NewKey[*Tensor]("input", VariantTensor)

	// KeyInputA is the first input for binary operations.
	KeyInputA = capitan.NewKey[*Tensor]("input_a", VariantTensor)

	// KeyInputB is the second input for binary operations.
	KeyInputB = capitan.NewKey[*Tensor]("input_b", VariantTensor)

	// KeyInputs is a list of input tensors for n-ary operations.
	KeyInputs = capitan.NewKey[[]*Tensor]("inputs", VariantTensorList)

	// KeyOutput is the output tensor.
	KeyOutput = capitan.NewKey[*Tensor]("output", VariantTensor)

	// KeyShape is the shape of a tensor.
	KeyShape = capitan.NewKey[[]int]("shape", VariantShape)

	// KeyDevice is the device of a tensor.
	KeyDevice = capitan.NewKey[Device]("device", VariantDevice)

	// KeyFromDevice is the source device for transfers.
	KeyFromDevice = capitan.NewKey[Device]("from_device", VariantDevice)

	// KeyToDevice is the destination device for transfers.
	KeyToDevice = capitan.NewKey[Device]("to_device", VariantDevice)

	// KeyDim is a dimension index.
	KeyDim = capitan.NewIntKey("dim")

	// KeyDims is multiple dimension indices.
	KeyDims = capitan.NewKey[[]int]("dims", VariantShape)

	// KeyBytes is a byte count.
	KeyBytes = capitan.NewIntKey("bytes")

	// KeyScalar is a scalar value.
	KeyScalar = capitan.NewFloat32Key("scalar")

	// KeyOpName is the operation name.
	KeyOpName = capitan.NewStringKey("op_name")

	// KeyTraceID is the pipeline trace identifier for operation correlation.
	KeyTraceID = capitan.NewStringKey("trace_id")

	// KeyStart is the start index for slice operations.
	KeyStart = capitan.NewIntKey("start")

	// KeyEnd is the end index for slice operations.
	KeyEnd = capitan.NewIntKey("end")

	// KeyDim0 is the first dimension for transpose operations.
	KeyDim0 = capitan.NewIntKey("dim0")

	// KeyDim1 is the second dimension for transpose operations.
	KeyDim1 = capitan.NewIntKey("dim1")

	// KeyPermutation is the dimension ordering for permute operations.
	KeyPermutation = capitan.NewKey[[]int]("permutation", VariantShape)

	// KeyPadding is the padding for convolution operations.
	KeyPadding = capitan.NewKey[[2]int]("padding", VariantIntPair)

	// KeyConvStride is the stride for convolution operations.
	KeyConvStride = capitan.NewKey[[2]int]("conv_stride", VariantIntPair)

	// KeyDilation is the dilation for convolution operations.
	KeyDilation = capitan.NewKey[[2]int]("dilation", VariantIntPair)

	// KeyGroups is the number of groups for grouped convolution.
	KeyGroups = capitan.NewIntKey("groups")

	// KeyKernelSize is the kernel size for pooling operations.
	KeyKernelSize = capitan.NewKey[[2]int]("kernel_size", VariantIntPair)

	// KeyPoolStride is the stride for pooling operations.
	KeyPoolStride = capitan.NewKey[[2]int]("pool_stride", VariantIntPair)

	// KeyIndices is the indices tensor (e.g., for embedding lookups).
	KeyIndices = capitan.NewKey[*Tensor]("indices", VariantTensor)

	// KeyEpsilon is the epsilon value for numerical stability in normalization.
	KeyEpsilon = capitan.NewFloat32Key("epsilon")

	// KeyMomentum is the momentum for running stats in batch normalization.
	KeyMomentum = capitan.NewFloat32Key("momentum")

	// KeyNormalizedShape is the shape for layer normalization.
	KeyNormalizedShape = capitan.NewKey[[]int]("normalized_shape", VariantShape)

	// KeyWeight is the weight parameter tensor.
	KeyWeight = capitan.NewKey[*Tensor]("weight", VariantTensor)

	// KeyBias is the bias parameter tensor.
	KeyBias = capitan.NewKey[*Tensor]("bias", VariantTensor)

	// KeyRunningMean is the running mean for batch normalization.
	KeyRunningMean = capitan.NewKey[*Tensor]("running_mean", VariantTensor)

	// KeyRunningVar is the running variance for batch normalization.
	KeyRunningVar = capitan.NewKey[*Tensor]("running_var", VariantTensor)

	// KeyMin is the minimum value for clamp operations.
	KeyMin = capitan.NewFloat32Key("min")

	// KeyMax is the maximum value for clamp operations.
	KeyMax = capitan.NewFloat32Key("max")

	// KeyCondition is the condition tensor for where operations.
	KeyCondition = capitan.NewKey[*Tensor]("condition", VariantTensor)

	// KeyOther is the alternative tensor for where operations.
	KeyOther = capitan.NewKey[*Tensor]("other", VariantTensor)

	// KeyTarget is the target tensor for loss functions.
	KeyTarget = capitan.NewKey[*Tensor]("target", VariantTensor)

	// KeyReduction is the reduction mode for loss functions ("none", "mean", "sum").
	KeyReduction = capitan.NewStringKey("reduction")

	// KeyK is the diagonal offset for tril operations.
	KeyK = capitan.NewIntKey("k")

	// KeyNumGroups is the number of groups for group normalization.
	KeyNumGroups = capitan.NewIntKey("num_groups")

	// KeyOutputSize is the target output size for adaptive pooling.
	KeyOutputSize = capitan.NewKey[[2]int]("output_size", VariantIntPair)

	// KeyOutputPadding is the output padding for transposed convolution.
	KeyOutputPadding = capitan.NewKey[[2]int]("output_padding", VariantIntPair)
)

Typed keys for event fields.

View Source
var (
	IdentityAdd    = pipz.NewIdentity("add", "Element-wise addition")
	IdentitySub    = pipz.NewIdentity("sub", "Element-wise subtraction")
	IdentityMul    = pipz.NewIdentity("mul", "Element-wise multiplication")
	IdentityDiv    = pipz.NewIdentity("div", "Element-wise division")
	IdentityNeg    = pipz.NewIdentity("neg", "Element-wise negation")
	IdentityAbs    = pipz.NewIdentity("abs", "Element-wise absolute value")
	IdentityExp    = pipz.NewIdentity("exp", "Element-wise exponential")
	IdentityLog    = pipz.NewIdentity("log", "Element-wise natural logarithm")
	IdentitySqrt   = pipz.NewIdentity("sqrt", "Element-wise square root")
	IdentitySquare = pipz.NewIdentity("square", "Element-wise square")
	IdentitySign   = pipz.NewIdentity("sign", "Element-wise sign")
	IdentityPow    = pipz.NewIdentity("pow", "Element-wise power")
	IdentityClamp  = pipz.NewIdentity("clamp", "Element-wise clamp to range")
	IdentityWhere  = pipz.NewIdentity("where", "Element-wise conditional selection")
	IdentitySin    = pipz.NewIdentity("sin", "Element-wise sine")
	IdentityCos    = pipz.NewIdentity("cos", "Element-wise cosine")
	IdentityTril   = pipz.NewIdentity("tril", "Lower triangular matrix extraction")
)

Element-wise operation identities.

View Source
var (
	IdentityReLU       = pipz.NewIdentity("relu", "ReLU activation")
	IdentitySigmoid    = pipz.NewIdentity("sigmoid", "Sigmoid activation")
	IdentityTanh       = pipz.NewIdentity("tanh", "Tanh activation")
	IdentityGELU       = pipz.NewIdentity("gelu", "GELU activation")
	IdentitySiLU       = pipz.NewIdentity("silu", "SiLU activation")
	IdentitySoftmax    = pipz.NewIdentity("softmax", "Softmax activation")
	IdentityLogSoftmax = pipz.NewIdentity("logsoftmax", "Log softmax activation")
	IdentityLeakyReLU  = pipz.NewIdentity("leaky_relu", "Leaky ReLU activation")
	IdentityDropout    = pipz.NewIdentity("dropout", "Dropout regularization")
)

Activation operation identities.

View Source
var (
	IdentityMatMul    = pipz.NewIdentity("matmul", "Matrix multiplication")
	IdentityTranspose = pipz.NewIdentity("transpose", "Matrix transpose")
	IdentityT         = pipz.NewIdentity("t", "Transpose last two dimensions")
)

Matrix operation identities.

View Source
var (
	IdentityReshape   = pipz.NewIdentity("reshape", "Reshape tensor")
	IdentityView      = pipz.NewIdentity("view", "View tensor with new shape")
	IdentitySqueeze   = pipz.NewIdentity("squeeze", "Squeeze dimensions")
	IdentityUnsqueeze = pipz.NewIdentity("unsqueeze", "Unsqueeze dimension")
	IdentityFlatten   = pipz.NewIdentity("flatten", "Flatten dimensions")
	IdentitySlice     = pipz.NewIdentity("slice", "Slice tensor")
	IdentityNarrow    = pipz.NewIdentity("narrow", "Narrow tensor along dimension")
	IdentityExpand    = pipz.NewIdentity("expand", "Expand/broadcast tensor")
	IdentityPermute   = pipz.NewIdentity("permute", "Permute dimensions")
	IdentityCat       = pipz.NewIdentity("cat", "Concatenate tensors")
	IdentityStack     = pipz.NewIdentity("stack", "Stack tensors")
)

Shape operation identities.

View Source
var (
	IdentitySum    = pipz.NewIdentity("sum", "Sum reduction")
	IdentityMean   = pipz.NewIdentity("mean", "Mean reduction")
	IdentityMax    = pipz.NewIdentity("max", "Max reduction")
	IdentityMin    = pipz.NewIdentity("min", "Min reduction")
	IdentityArgMax = pipz.NewIdentity("argmax", "Argument maximum reduction")
	IdentityArgMin = pipz.NewIdentity("argmin", "Argument minimum reduction")
	IdentityVar    = pipz.NewIdentity("var", "Variance reduction")
	IdentityStd    = pipz.NewIdentity("std", "Standard deviation reduction")
	IdentityProd   = pipz.NewIdentity("prod", "Product reduction")
)

Reduction operation identities.

View Source
var (
	IdentityBatchNorm2d    = pipz.NewIdentity("batchnorm2d", "Batch normalization over 4D input")
	IdentityLayerNorm      = pipz.NewIdentity("layernorm", "Layer normalization")
	IdentityRMSNorm        = pipz.NewIdentity("rmsnorm", "Root mean square normalization")
	IdentityGroupNorm      = pipz.NewIdentity("groupnorm", "Group normalization")
	IdentityInstanceNorm2d = pipz.NewIdentity("instancenorm2d", "Instance normalization over 4D input")
)

Normalization operation identities.

View Source
var (
	IdentityConv2d          = pipz.NewIdentity("conv2d", "2D convolution")
	IdentityConvTranspose2d = pipz.NewIdentity("convtranspose2d", "2D transposed convolution")
)

Convolution operation identities.

View Source
var (
	IdentityMaxPool2d         = pipz.NewIdentity("maxpool2d", "2D max pooling")
	IdentityAvgPool2d         = pipz.NewIdentity("avgpool2d", "2D average pooling")
	IdentityAdaptiveAvgPool2d = pipz.NewIdentity("adaptiveavgpool2d", "Adaptive 2D average pooling")
	IdentityAdaptiveMaxPool2d = pipz.NewIdentity("adaptivemaxpool2d", "Adaptive 2D max pooling")
)

Pooling operation identities.

View Source
var (
	IdentityMSELoss          = pipz.NewIdentity("mse_loss", "Mean squared error loss")
	IdentityL1Loss           = pipz.NewIdentity("l1_loss", "L1 loss")
	IdentityCrossEntropyLoss = pipz.NewIdentity("cross_entropy", "Cross entropy loss")
	IdentityNLLLoss          = pipz.NewIdentity("nll_loss", "Negative log likelihood loss")
)

Loss function identities.

View Source
var (
	// ErrNoBackend is returned when a required backend is not available.
	ErrNoBackend = &BackendError{Message: "backend not available"}
	// ErrCUDANotAvailable is returned when CUDA operations are attempted without CUDA support.
	ErrCUDANotAvailable = &BackendError{Message: "CUDA not available: no CUDA devices found"}
)

Common errors.

View Source
var (
	KeySourceDevice = capitan.NewKey[Device]("source_device", VariantDevice)
	KeyTargetDevice = capitan.NewKey[Device]("target_device", VariantDevice)
)

Signal field keys for transfer.

View Source
var (
	IdentityEmbedding = pipz.NewIdentity("embedding", "Embedding lookup")
)

Embedding operation identities.

View Source
var (
	IdentityTransfer = pipz.NewIdentity("transfer", "Device transfer")
)

Transfer operation identities.

View Source
var (
	OpConv2d = capitan.NewSignal("tendo.op.conv2d", "2D convolution")
)

Observability signals for convolution.

View Source
var (
	OpTransfer = capitan.NewSignal("tendo.op.transfer", "Device transfer")
)

Observability signals for device transfer.

Functions

func BFloat

func BFloat() pipz.Chainable[*Tensor]

BFloat is an alias for ToBFloat16.

func BFloat16SliceToFloat32

func BFloat16SliceToFloat32(bf16 []uint16) []float32

BFloat16SliceToFloat32 converts a slice of bfloat16 to float32.

func BFloat16ToFloat32

func BFloat16ToFloat32(b uint16) float32

BFloat16ToFloat32 converts a bfloat16 to float32. Simply places the 16 bits in the upper half of float32.

func BinaryOp

func BinaryOp(identity pipz.Identity, other *Tensor, fn func(*CPUStorage, *CPUStorage, []int) *CPUStorage) pipz.Chainable[*Tensor]

BinaryOp creates a binary tensor operation with the given name. The second operand is captured in the closure.

func BroadcastShapes

func BroadcastShapes(a, b []int) ([]int, error)

BroadcastShapes computes the broadcast shape of two shapes. Returns an error if the shapes are not broadcastable.

func CanBroadcast

func CanBroadcast(a, b []int) bool

CanBroadcast returns true if two shapes can be broadcast together.

func ComputeStrides

func ComputeStrides(shape []int) []int

ComputeStrides computes the strides for a contiguous tensor with the given shape. Strides are in row-major (C) order.

func Conv2dOutputShape

func Conv2dOutputShape(inputShape, weightShape []int, config Conv2dConfig) []int

Conv2dOutputShape computes the output shape for a conv2d operation.

func CoordsToFlat

func CoordsToFlat(coords, strides []int) int

CoordsToFlat converts multi-dimensional coordinates to a flat index using strides.

func Fallback

func Fallback(name string, processors ...pipz.Chainable[*Tensor]) *pipz.Fallback[*Tensor]

Fallback creates a processor that tries alternatives on failure. Each processor is tried in order until one succeeds. Useful for GPU to CPU fallback.

Example:

resilient := tendo.Fallback("matmul",
    cudaMatMul,   // Try GPU first
    cpuMatMul,    // Fall back to CPU
)

func Filter

func Filter(name string, predicate func(context.Context, *Tensor) bool, processor pipz.Chainable[*Tensor]) *pipz.Filter[*Tensor]

Filter creates a conditional processor that either processes or passes through. When the predicate returns true, the processor is executed. When false, the tensor passes through unchanged.

Example:

gpuOnly := tendo.Filter("gpu-only",
    func(ctx context.Context, t *tendo.Tensor) bool {
        return t.Device().Type == tendo.CUDA
    },
    gpuProcessor,
)

func FlatToCoords

func FlatToCoords(flat int, shape []int) []int

FlatToCoords converts a flat index to multi-dimensional coordinates.

func FlatToReducedIndex

func FlatToReducedIndex(flat int, shape, strides, outStrides []int, skipDims map[int]bool) int

FlatToReducedIndex maps an input flat index to an output index after removing specified dimensions. strides should be from ComputeStrides(shape) for contiguous data. skipDims is a set of dimensions to exclude from the output index.

func FlatToStrided

func FlatToStrided(flat int, shape, stride []int, offset int) int

FlatToStrided converts a flat (contiguous) index to a strided storage index. This handles non-contiguous tensors by computing the physical storage location from a logical element position.

func Float

func Float() pipz.Chainable[*Tensor]

Float is an alias for ToFloat32.

func Float16SliceToFloat32

func Float16SliceToFloat32(f16 []uint16) []float32

Float16SliceToFloat32 converts a slice of float16 to float32.

func Float16ToFloat32

func Float16ToFloat32(h uint16) float32

Float16ToFloat32 converts a float16 (IEEE 754 half-precision) to float32.

func Float32SliceToBFloat16

func Float32SliceToBFloat16(f32 []float32) []uint16

Float32SliceToBFloat16 converts a slice of float32 to bfloat16.

func Float32SliceToFloat16

func Float32SliceToFloat16(f32 []float32) []uint16

Float32SliceToFloat16 converts a slice of float32 to float16.

func Float32ToBFloat16

func Float32ToBFloat16(f float32) uint16

Float32ToBFloat16 converts a float32 to bfloat16. BFloat16 is the upper 16 bits of float32 with rounding.

func Float32ToFloat16

func Float32ToFloat16(f float32) uint16

Float32ToFloat16 converts a float32 to float16 (IEEE 754 half-precision). Returns the 16-bit representation as uint16.

func Half

func Half() pipz.Chainable[*Tensor]

Half is an alias for ToFloat16.

func InferShape

func InferShape(numel int, shape []int) ([]int, error)

InferShape infers the full shape when one dimension is -1. Returns an error if more than one -1 is present or if the shape is invalid.

func IsContiguous

func IsContiguous(shape, stride []int) bool

IsContiguous returns true if the given shape and stride represent contiguous memory.

func IsTraining

func IsTraining(ctx context.Context) bool

IsTraining returns true if the context has training mode enabled.

func IterateSlices

func IterateSlices(shape, strides []int, dim int, fn func(baseIdx, dimStride int))

IterateSlices iterates over all slices along a dimension. For each slice, calls fn with the base storage index and dim stride. Use: for j := 0; j < shape[dim]; j++ { idx := baseIdx + j*dimStride }.

func IterateStrided

func IterateStrided(numel int, shape, stride []int, offset int, fn func(flatIdx, stridedIdx int))

IterateStrided calls fn for each element, providing both the logical flat index and the physical strided storage index. Useful for iterating non-contiguous tensors.

func MakeContiguous

func MakeContiguous() pipz.Chainable[*Tensor]

MakeContiguous returns a Chainable that makes the tensor contiguous in memory. If already contiguous, returns the tensor unchanged.

func Numel

func Numel(shape []int) int

Numel returns the total number of elements for a given shape.

func Op

func Op(identity pipz.Identity, fn func(context.Context, *Tensor) (*Tensor, error)) pipz.Chainable[*Tensor]

Op creates a named Chainable operation that can fail. This is a convenience wrapper around pipz.Apply.

func Pin

func Pin() pipz.Chainable[*Tensor]

Pin returns a Chainable that pins the tensor's memory for faster CPU-GPU transfer. Only applicable to CPU tensors. No-op if already pinned or on GPU.

func ReduceShapeAndStrides

func ReduceShapeAndStrides(shape []int, skipDims map[int]bool) (outShape, outStrides []int)

ReduceShapeAndStrides computes the output shape and strides after removing dimensions.

func RegisterBackend

func RegisterBackend(b StorageBackend)

RegisterBackend registers a storage backend.

func RegisterMemoryAllocator

func RegisterMemoryAllocator(dt DeviceType, alloc MemoryAllocator)

RegisterMemoryAllocator registers a memory allocator for a device type.

func Sequence

func Sequence(name string, processors ...pipz.Chainable[*Tensor]) *pipz.Sequence[*Tensor]

Sequence creates a sequential pipeline of tensor processors. Each processor receives the output of the previous one.

Example:

pipeline := tendo.Sequence("preprocess",
    tendo.NewReshape(-1, 224, 224, 3),
    tendo.NewPermute(0, 3, 1, 2),  // NHWC -> NCHW
    normalize,
)

func SetDefaultDType

func SetDefaultDType(d DType)

SetDefaultDType sets the default data type for tensor creation.

func SetDefaultPool

func SetDefaultPool(p *Pool)

SetDefaultPool sets the default memory pool.

func SliceBaseIndex

func SliceBaseIndex(sliceIdx int, shape, strides []int, dim int) int

SliceBaseIndex computes the base storage index for a slice along a dimension. sliceIdx is the index in the reduced shape (with dim removed), and the returned baseIdx is the starting position in storage for iterating along dim.

func SqueezeShape

func SqueezeShape(shape []int, dim int) ([]int, error)

SqueezeShape removes a dimension of size 1 from the shape.

func Switch

func Switch(name string, condition func(context.Context, *Tensor) string) *pipz.Switch[*Tensor]

Switch creates a router that directs tensors to different processors. The condition function returns a route key that determines which processor handles the tensor.

Example:

router := tendo.Switch("device-router", func(ctx context.Context, t *tendo.Tensor) string {
    return string(t.Device().Type)
})
router.AddRoute("cpu", cpuProcessor)
router.AddRoute("cuda", cudaProcessor)

func Sync

func Sync() pipz.Chainable[*Tensor]

Sync returns a Chainable that synchronizes the tensor's device. For CUDA tensors, this blocks until all pending operations complete. For CPU tensors, this is a no-op.

func Timeout

func Timeout(name string, processor pipz.Chainable[*Tensor], duration time.Duration) *pipz.Timeout[*Tensor]

Timeout creates a processor that enforces a time limit on execution. If the timeout expires, the operation is canceled and an error is returned. Useful for SLA enforcement in inference services.

Example:

bounded := tendo.Timeout("inference", model, 100*time.Millisecond)

func To

func To(device Device) pipz.Chainable[*Tensor]

To returns a Chainable that moves the tensor to the specified device. If already on the target device, returns the tensor unchanged.

func ToBFloat16

func ToBFloat16() pipz.Chainable[*Tensor]

ToBFloat16 returns a Chainable that converts a tensor to BFloat16.

func ToCPU

func ToCPU() pipz.Chainable[*Tensor]

ToCPU returns a Chainable that moves the tensor to CPU.

func ToDType

func ToDType(dtype DType) pipz.Chainable[*Tensor]

ToDType returns a Chainable that converts a tensor to the specified dtype.

func ToFloat16

func ToFloat16() pipz.Chainable[*Tensor]

ToFloat16 returns a Chainable that converts a tensor to Float16.

func ToFloat32

func ToFloat32() pipz.Chainable[*Tensor]

ToFloat32 returns a Chainable that converts a tensor to Float32.

func ToGPU

func ToGPU(index int) pipz.Chainable[*Tensor]

ToGPU returns a Chainable that moves the tensor to the specified GPU.

func TraceID

func TraceID(ctx context.Context) string

TraceID returns the trace ID from the context, or empty string if not set.

func Transform

func Transform(identity pipz.Identity, fn func(context.Context, *Tensor) *Tensor) pipz.Chainable[*Tensor]

Transform creates a named Chainable operation that cannot fail. This is a convenience wrapper around pipz.Transform.

func TransposeShape

func TransposeShape(shape []int, dim0, dim1 int) ([]int, error)

TransposeShape returns the shape after transposing two dimensions.

func TransposeStride

func TransposeStride(stride []int, dim0, dim1 int) ([]int, error)

TransposeStride returns the strides after transposing two dimensions.

func UnaryOp

func UnaryOp(identity pipz.Identity, fn func(*CPUStorage) *CPUStorage) pipz.Chainable[*Tensor]

UnaryOp creates a unary tensor operation with the given name, signal, and compute function. The compute function operates on CPU storage directly.

func UnaryOpGeneric

func UnaryOpGeneric(identity pipz.Identity, fn func(CPUDataAccessor) *CPUStorage) pipz.Chainable[*Tensor]

UnaryOpGeneric creates a unary tensor operation that works with any CPU storage.

func UnaryOpInPlace

func UnaryOpInPlace(identity pipz.Identity, fn func(*CPUStorage)) pipz.Chainable[*Tensor]

UnaryOpInPlace creates an in-place unary tensor operation. The input tensor is modified and returned.

func Unpin

func Unpin() pipz.Chainable[*Tensor]

Unpin returns a Chainable that unpins the tensor's memory.

func UnsqueezeShape

func UnsqueezeShape(shape []int, dim int) ([]int, error)

UnsqueezeShape inserts a dimension of size 1 at the given position.

func ValidateElementwise

func ValidateElementwise(a, b []int) ([]int, error)

ValidateElementwise validates shapes for element-wise operations and returns the output shape.

func ValidateMatMul

func ValidateMatMul(a, b []int) ([]int, error)

ValidateMatMul validates shapes for matrix multiplication and returns the output shape. Supports 2D matrices and batched matrix multiplication.

func WithInference

func WithInference(ctx context.Context) context.Context

WithInference returns a context with training mode disabled (inference mode). This is the default behavior.

func WithPool

func WithPool(ctx context.Context, p *Pool) context.Context

WithPool returns a context with the given memory pool. Operations using this context will allocate from the pool.

func WithTraceID

func WithTraceID(ctx context.Context, id string) context.Context

WithTraceID returns a context with the given trace ID. All operations executed with this context will include the trace ID in their emitted events, enabling correlation of operations for debugging and profiling.

func WithTraining

func WithTraining(ctx context.Context) context.Context

WithTraining returns a context with training mode enabled. Operations like Dropout will apply their training behavior.

Types

type Abs

type Abs struct {
	// contains filtered or unexported fields
}

Abs is a chainable operator that takes the absolute value of each element.

func NewAbs

func NewAbs(backend UnaryOps) *Abs

NewAbs creates an Abs operator.

func (*Abs) Close

func (a *Abs) Close() error

Close releases any resources held by this operator.

func (*Abs) Identity

func (a *Abs) Identity() pipz.Identity

Identity returns the operator identity.

func (*Abs) Process

func (a *Abs) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the absolute value of each element.

func (*Abs) Schema

func (a *Abs) Schema() pipz.Node

Schema returns the operator schema.

type ActivationOps

type ActivationOps interface {
	Softmax(ctx context.Context, t *Tensor, dim int) (*Tensor, error)
	LogSoftmax(ctx context.Context, t *Tensor, dim int) (*Tensor, error)
	LeakyReLU(ctx context.Context, t *Tensor, negativeSlope float32) (*Tensor, error)
	// Dropout randomly zeros elements with probability p during training.
	// During inference (training=false), returns input unchanged.
	Dropout(ctx context.Context, t *Tensor, p float32, training bool) (*Tensor, error)
}

ActivationOps defines parameterized activation operations.

type AdaptiveAvgPool2d

type AdaptiveAvgPool2d struct {
	// contains filtered or unexported fields
}

AdaptiveAvgPool2d is a chainable operator that applies adaptive 2D average pooling. The output has a fixed spatial size regardless of input size. Input shape: [N, C, H, W] or [C, H, W].

func NewAdaptiveAvgPool2d

func NewAdaptiveAvgPool2d(backend PoolOps, outputSize [2]int) *AdaptiveAvgPool2d

NewAdaptiveAvgPool2d creates an AdaptiveAvgPool2d operator.

func (*AdaptiveAvgPool2d) Close

func (a *AdaptiveAvgPool2d) Close() error

Close releases any resources held by this operator.

func (*AdaptiveAvgPool2d) Identity

func (a *AdaptiveAvgPool2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*AdaptiveAvgPool2d) Process

func (a *AdaptiveAvgPool2d) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies adaptive average pooling.

func (*AdaptiveAvgPool2d) Schema

func (a *AdaptiveAvgPool2d) Schema() pipz.Node

Schema returns the operator schema node.

type AdaptiveMaxPool2d

type AdaptiveMaxPool2d struct {
	// contains filtered or unexported fields
}

AdaptiveMaxPool2d is a chainable operator that applies adaptive 2D max pooling. The output has a fixed spatial size regardless of input size. Input shape: [N, C, H, W] or [C, H, W].

func NewAdaptiveMaxPool2d

func NewAdaptiveMaxPool2d(backend PoolOps, outputSize [2]int) *AdaptiveMaxPool2d

NewAdaptiveMaxPool2d creates an AdaptiveMaxPool2d operator.

func (*AdaptiveMaxPool2d) Close

func (a *AdaptiveMaxPool2d) Close() error

Close releases any resources held by this operator.

func (*AdaptiveMaxPool2d) Identity

func (a *AdaptiveMaxPool2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*AdaptiveMaxPool2d) Process

func (a *AdaptiveMaxPool2d) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies adaptive max pooling.

func (*AdaptiveMaxPool2d) Schema

func (a *AdaptiveMaxPool2d) Schema() pipz.Node

Schema returns the operator schema node.

type Add

type Add struct {
	// contains filtered or unexported fields
}

Add is a chainable operator that performs element-wise addition.

func NewAdd

func NewAdd(backend BinaryOps, other *Tensor) *Add

NewAdd creates an Add operator.

func (*Add) Close

func (a *Add) Close() error

Close releases any resources held by this operator.

func (*Add) Identity

func (a *Add) Identity() pipz.Identity

Identity returns the operator identity.

func (*Add) Process

func (a *Add) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process performs element-wise addition.

func (*Add) Schema

func (a *Add) Schema() pipz.Node

Schema returns the operator schema.

type ArgMax

type ArgMax struct {
	// contains filtered or unexported fields
}

ArgMax is a chainable operator that returns indices of max values along a dimension.

func NewArgMax

func NewArgMax(backend ReduceOps, dim int, keepdim bool) *ArgMax

NewArgMax creates an ArgMax operator.

func (*ArgMax) Close

func (a *ArgMax) Close() error

Close releases any resources held by this operator.

func (*ArgMax) Identity

func (a *ArgMax) Identity() pipz.Identity

Identity returns the operator identity.

func (*ArgMax) Process

func (a *ArgMax) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes indices of max values along the dimension.

func (*ArgMax) Schema

func (a *ArgMax) Schema() pipz.Node

Schema returns the operator schema.

type ArgMin

type ArgMin struct {
	// contains filtered or unexported fields
}

ArgMin is a chainable operator that returns indices of min values along a dimension.

func NewArgMin

func NewArgMin(backend ReduceOps, dim int, keepdim bool) *ArgMin

NewArgMin creates an ArgMin operator.

func (*ArgMin) Close

func (a *ArgMin) Close() error

Close releases any resources held by this operator.

func (*ArgMin) Identity

func (a *ArgMin) Identity() pipz.Identity

Identity returns the operator identity.

func (*ArgMin) Process

func (a *ArgMin) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes indices of min values along the dimension.

func (*ArgMin) Schema

func (a *ArgMin) Schema() pipz.Node

Schema returns the operator schema.

type ArithmeticBackend

type ArithmeticBackend interface {
	UnaryOps
	BinaryOps
}

ArithmeticBackend provides element-wise arithmetic operations.

type AvgPool2d

type AvgPool2d struct {
	// contains filtered or unexported fields
}

AvgPool2d is a chainable operator that applies 2D average pooling. Input shape: [N, C, H, W] or [C, H, W] Output shape: [N, C, H', W'] or [C, H', W'].

func NewAvgPool2d

func NewAvgPool2d(backend PoolOps, config Pool2dConfig) *AvgPool2d

NewAvgPool2d creates an AvgPool2d operator.

func (*AvgPool2d) Close

func (a *AvgPool2d) Close() error

Close releases any resources held by this operator.

func (*AvgPool2d) Identity

func (a *AvgPool2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*AvgPool2d) Process

func (a *AvgPool2d) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies 2D average pooling.

func (*AvgPool2d) Schema

func (a *AvgPool2d) Schema() pipz.Node

Schema returns the operator schema node.

type Backend

Backend is the unified interface for compute backends. It combines all capability interfaces for full functionality.

type BackendError

type BackendError struct {
	Message string
}

BackendError represents a backend-related error.

func (*BackendError) Error

func (e *BackendError) Error() string

func (*BackendError) Is

func (e *BackendError) Is(target error) bool

Is reports whether target matches this BackendError by message.

type BatchNorm2d

type BatchNorm2d struct {
	// contains filtered or unexported fields
}

BatchNorm2d is a chainable operator that applies batch normalization over 4D input. Input shape: [N, C, H, W] Normalizes over N, H, W dimensions per channel.

func NewBatchNorm2d

func NewBatchNorm2d(backend NormOps, weight, bias, runningMean, runningVar *Tensor, epsilon, momentum float32) *BatchNorm2d

NewBatchNorm2d creates a BatchNorm2d operator.

func (*BatchNorm2d) Close

func (b *BatchNorm2d) Close() error

Close releases any resources held by this operator.

func (*BatchNorm2d) Identity

func (b *BatchNorm2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*BatchNorm2d) Process

func (b *BatchNorm2d) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies batch normalization.

func (*BatchNorm2d) Schema

func (b *BatchNorm2d) Schema() pipz.Node

Schema returns the operator schema node.

type BinaryOps

type BinaryOps interface {
	Add(ctx context.Context, a, b *Tensor) (*Tensor, error)
	Sub(ctx context.Context, a, b *Tensor) (*Tensor, error)
	Mul(ctx context.Context, a, b *Tensor) (*Tensor, error)
	Div(ctx context.Context, a, b *Tensor) (*Tensor, error)
	Pow(ctx context.Context, t *Tensor, exp float32) (*Tensor, error)
}

BinaryOps defines element-wise binary operations.

type CPUDataAccessor

type CPUDataAccessor interface {
	// Data returns the underlying float32 slice for direct access.
	// Panics if the storage contains int64 data.
	Data() []float32

	// SetData sets the value at the given index.
	SetData(index int, value float32)

	// GetData gets the value at the given index.
	GetData(index int) float32

	// Fill sets all elements to the given value.
	Fill(value float32)
}

CPUDataAccessor is an interface for CPU storage types that provide direct data access. This allows helper functions to work with different CPU storage implementations.

type CPUInt64DataAccessor

type CPUInt64DataAccessor interface {
	// Int64Data returns the underlying int64 slice for direct access.
	// Panics if the storage contains float32 data.
	Int64Data() []int64
}

CPUInt64DataAccessor is an interface for CPU storage types with int64 data.

type CPUStorage

type CPUStorage struct {
	// contains filtered or unexported fields
}

CPUStorage stores tensor data in CPU memory. This is the core CPU storage implementation used by internal operations. Supports both float32 data (for compute) and int64 data (for indices).

func AllocCPUFromContext

func AllocCPUFromContext(ctx context.Context, numel int, dtype DType) *CPUStorage

AllocCPUFromContext allocates CPU storage from the pool in the context.

func NewCPUInt64StorageFromSlice

func NewCPUInt64StorageFromSlice(data []int64) *CPUStorage

NewCPUInt64StorageFromSlice creates a CPU storage from an existing int64 slice. The slice is copied, not referenced.

func NewCPUStorage

func NewCPUStorage(numel int, dtype DType) *CPUStorage

NewCPUStorage creates a new CPU storage with the given number of elements.

func NewCPUStorageFromSlice

func NewCPUStorageFromSlice(data []float32, dtype DType) *CPUStorage

NewCPUStorageFromSlice creates a CPU storage from an existing float32 slice. The slice is copied, not referenced.

func (*CPUStorage) Clone

func (s *CPUStorage) Clone() Storage

Clone creates a deep copy of the storage.

func (*CPUStorage) DType

func (s *CPUStorage) DType() DType

DType returns the data type.

func (*CPUStorage) Data

func (s *CPUStorage) Data() []float32

Data returns the underlying float32 slice for direct access. Panics if the storage contains int64 data.

func (*CPUStorage) Device

func (s *CPUStorage) Device() Device

Device returns the CPU device.

func (*CPUStorage) Fill

func (s *CPUStorage) Fill(value float32)

Fill sets all elements to the given value.

func (*CPUStorage) Free

func (s *CPUStorage) Free()

Free releases the storage. For CPU storage, this is a no-op since Go's GC handles memory management.

func (*CPUStorage) GetData

func (s *CPUStorage) GetData(index int) float32

GetData gets the value at the given index.

func (*CPUStorage) Int64Data

func (s *CPUStorage) Int64Data() []int64

Int64Data returns the underlying int64 slice for direct access. Panics if the storage contains float32 data.

func (*CPUStorage) Len

func (s *CPUStorage) Len() int

Len returns the number of elements in the storage.

func (*CPUStorage) PoolKey

func (s *CPUStorage) PoolKey() (numel int, dtype DType, deviceIndex int)

PoolKey returns the metadata needed for pool operations.

func (*CPUStorage) Ptr

func (s *CPUStorage) Ptr() uintptr

Ptr returns the raw pointer to the data.

func (*CPUStorage) SetData

func (s *CPUStorage) SetData(index int, value float32)

SetData sets the value at the given index.

func (*CPUStorage) Size

func (s *CPUStorage) Size() int

Size returns the size in bytes of the allocated memory.

type CUDAError

type CUDAError struct {
	Message string
	Code    int
}

CUDAError represents a CUDA-related error.

func (*CUDAError) Error

func (e *CUDAError) Error() string

type Cat

type Cat struct {
	// contains filtered or unexported fields
}

Cat is a chainable operator that concatenates tensors along a dimension. All tensors must have the same shape except in the concatenation dimension.

func NewCat

func NewCat(backend ShapeOps, tensors []*Tensor, dim int) *Cat

NewCat creates a Cat operator. If backend is nil, uses CPU-only fallback implementation.

func (*Cat) Close

func (c *Cat) Close() error

Close releases any resources held by this operator.

func (*Cat) Identity

func (c *Cat) Identity() pipz.Identity

Identity returns the operator identity.

func (*Cat) Process

func (c *Cat) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process concatenates tensors.

func (*Cat) Schema

func (c *Cat) Schema() pipz.Node

Schema returns the operator schema.

type Clamp

type Clamp struct {
	// contains filtered or unexported fields
}

Clamp is a chainable operator that clamps values to a range.

func NewClamp

func NewClamp(backend CompareOps, minVal, maxVal float32) *Clamp

NewClamp creates a Clamp operator.

func (*Clamp) Close

func (c *Clamp) Close() error

Close releases any resources held by this operator.

func (*Clamp) Identity

func (c *Clamp) Identity() pipz.Identity

Identity returns the operator identity.

func (*Clamp) Process

func (c *Clamp) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process clamps values to [min, max].

func (*Clamp) Schema

func (c *Clamp) Schema() pipz.Node

Schema returns the operator schema.

type CompareBackend

type CompareBackend interface {
	CompareOps
}

CompareBackend provides comparison and selection operations.

type CompareOps

type CompareOps interface {
	Clamp(ctx context.Context, t *Tensor, minVal, maxVal float32) (*Tensor, error)
	Where(ctx context.Context, condition, x, y *Tensor) (*Tensor, error)

	// Tril returns lower triangular part of a 2D tensor.
	// Elements above diagonal + k are zeroed.
	// k=0: main diagonal, k<0: below, k>0: above.
	Tril(ctx context.Context, t *Tensor, k int) (*Tensor, error)
}

CompareOps defines comparison and selection operations.

type ContiguousMaker

type ContiguousMaker interface {
	// MakeContiguous copies strided data to new contiguous storage.
	// Returns storage containing only the logical elements.
	MakeContiguous(shape, stride []int, offset int) (Storage, error)
}

ContiguousMaker creates contiguous copies of strided tensor data. Storage types that can perform strided-to-contiguous copies on their native device should implement this interface.

type Conv2d

type Conv2d struct {
	// contains filtered or unexported fields
}

Conv2d is a chainable operator that applies 2D convolution. Input shape: [N, C_in, H, W] Weight shape: [C_out, C_in/groups, kH, kW] Output shape: [N, C_out, H_out, W_out].

func NewConv2d

func NewConv2d(backend ConvOps, weight *Tensor, config Conv2dConfig) *Conv2d

NewConv2d creates a Conv2d operator.

func NewConv2dSimple

func NewConv2dSimple(backend ConvOps, weight *Tensor, stride int) *Conv2d

NewConv2dSimple is a convenience constructor for standard convolution. Applies same padding to keep spatial dimensions.

func (*Conv2d) Close

func (c *Conv2d) Close() error

Close releases any resources held by this operator.

func (*Conv2d) Identity

func (c *Conv2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*Conv2d) Process

func (c *Conv2d) Process(ctx context.Context, input *Tensor) (*Tensor, error)

Process applies 2D convolution.

func (*Conv2d) Schema

func (c *Conv2d) Schema() pipz.Node

Schema returns the operator schema.

type Conv2dConfig

type Conv2dConfig struct {
	Padding  [2]int // [padH, padW]
	Stride   [2]int // [strideH, strideW]
	Dilation [2]int // [dilationH, dilationW]
	Groups   int    // number of groups for grouped convolution
}

Conv2dConfig holds configuration for 2D convolution.

func DefaultConv2dConfig

func DefaultConv2dConfig() Conv2dConfig

DefaultConv2dConfig returns default convolution config.

type ConvOps

type ConvOps interface {
	Conv2d(ctx context.Context, input, weight *Tensor, padding, stride, dilation [2]int, groups int) (*Tensor, error)
	ConvTranspose2d(ctx context.Context, input, weight *Tensor, padding, outputPadding, stride, dilation [2]int, groups int) (*Tensor, error)
}

ConvOps defines convolution operations.

type ConvTranspose2d

type ConvTranspose2d struct {
	// contains filtered or unexported fields
}

ConvTranspose2d is a chainable operator that applies 2D transposed convolution. Also known as deconvolution or fractionally-strided convolution. Input shape: [N, C_in, H, W] Weight shape: [C_in, C_out/groups, kH, kW] Output shape: [N, C_out, H_out, W_out].

func NewConvTranspose2d

func NewConvTranspose2d(backend ConvOps, weight *Tensor, config ConvTranspose2dConfig) *ConvTranspose2d

NewConvTranspose2d creates a ConvTranspose2d operator.

func (*ConvTranspose2d) Close

func (c *ConvTranspose2d) Close() error

Close releases any resources held by this operator.

func (*ConvTranspose2d) Identity

func (c *ConvTranspose2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*ConvTranspose2d) Process

func (c *ConvTranspose2d) Process(ctx context.Context, input *Tensor) (*Tensor, error)

Process applies 2D transposed convolution.

func (*ConvTranspose2d) Schema

func (c *ConvTranspose2d) Schema() pipz.Node

Schema returns the operator schema.

type ConvTranspose2dConfig

type ConvTranspose2dConfig struct {
	Padding       [2]int // [padH, padW]
	OutputPadding [2]int // [outPadH, outPadW]
	Stride        [2]int // [strideH, strideW]
	Dilation      [2]int // [dilationH, dilationW]
	Groups        int    // number of groups for grouped convolution
}

ConvTranspose2dConfig holds configuration for 2D transposed convolution.

func DefaultConvTranspose2dConfig

func DefaultConvTranspose2dConfig() ConvTranspose2dConfig

DefaultConvTranspose2dConfig returns default transposed convolution config.

type CoreBackend

type CoreBackend interface {
	StorageOps
	DeviceInfo
	TensorFactory
}

CoreBackend is the minimal interface required by all backends. It provides storage, device information, and tensor creation.

type Cos

type Cos struct {
	// contains filtered or unexported fields
}

Cos is a chainable operator that computes the cosine of each element.

func NewCos

func NewCos(backend UnaryOps) *Cos

NewCos creates a Cos operator.

func (*Cos) Close

func (c *Cos) Close() error

Close releases any resources held by this operator.

func (*Cos) Identity

func (c *Cos) Identity() pipz.Identity

Identity returns the operator identity.

func (*Cos) Process

func (c *Cos) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the cosine of each element.

func (*Cos) Schema

func (c *Cos) Schema() pipz.Node

Schema returns the operator schema.

type CrossEntropyLoss

type CrossEntropyLoss struct {
	// contains filtered or unexported fields
}

CrossEntropyLoss is a chainable operator that computes cross-entropy loss. Input: [N, C] logits (not softmaxed) Target: [N] class indices (integers as float32) Loss = -log(softmax(input)[target]).

func NewCrossEntropyLoss

func NewCrossEntropyLoss(backend LossOps, target *Tensor, reduction string) *CrossEntropyLoss

NewCrossEntropyLoss creates a CrossEntropyLoss operator.

func (*CrossEntropyLoss) Close

func (c *CrossEntropyLoss) Close() error

Close releases any resources held by this operator.

func (*CrossEntropyLoss) Identity

func (c *CrossEntropyLoss) Identity() pipz.Identity

Identity returns the operator identity.

func (*CrossEntropyLoss) Process

func (c *CrossEntropyLoss) Process(ctx context.Context, input *Tensor) (*Tensor, error)

Process computes cross-entropy loss.

func (*CrossEntropyLoss) Schema

func (c *CrossEntropyLoss) Schema() pipz.Node

Schema returns the operator schema.

type DType

type DType uint8

DType represents the data type of tensor elements.

const (
	// Float32 is 32-bit floating point.
	Float32 DType = iota
	// Float16 is 16-bit floating point.
	Float16
	// BFloat16 is brain floating point (16-bit with 8-bit exponent).
	BFloat16
	// Int64 is 64-bit signed integer, used for indices.
	Int64
)

func DefaultDType

func DefaultDType() DType

DefaultDType returns the current default data type.

func (DType) Size

func (d DType) Size() int

Size returns the size in bytes of one element of this dtype.

func (DType) String

func (d DType) String() string

String returns the string representation of the dtype.

type Device

type Device struct {
	Type  DeviceType
	Index int
}

Device represents a compute device (CPU or GPU).

func CPUDevice

func CPUDevice() Device

CPUDevice returns the default CPU device.

func CUDADevice

func CUDADevice(index int) Device

CUDADevice returns a CUDA device with the given index.

func (Device) IsCPU

func (d Device) IsCPU() bool

IsCPU returns true if this is a CPU device.

func (Device) IsCUDA

func (d Device) IsCUDA() bool

IsCUDA returns true if this is a CUDA device.

func (Device) String

func (d Device) String() string

String returns the string representation of the device.

type DeviceError

type DeviceError struct {
	Expected DeviceType
	Got      DeviceType
}

DeviceError indicates an operation was attempted on an unsupported device.

func (*DeviceError) Error

func (e *DeviceError) Error() string

func (*DeviceError) Is

func (e *DeviceError) Is(target error) bool

Is reports whether target is a DeviceError.

type DeviceInfo

type DeviceInfo interface {
	// DeviceType returns the type of device this backend uses.
	DeviceType() DeviceType

	// IsAvailable returns true if the backend is functional.
	IsAvailable() bool

	// DeviceCount returns the number of devices available.
	DeviceCount() int
}

DeviceInfo provides information about the backend's device.

type DeviceMismatchError

type DeviceMismatchError struct {
	A Device
	B Device
}

DeviceMismatchError indicates two tensors are on different devices.

func (*DeviceMismatchError) Error

func (e *DeviceMismatchError) Error() string

func (*DeviceMismatchError) Is

func (e *DeviceMismatchError) Is(target error) bool

Is reports whether target is a DeviceMismatchError.

type DeviceType

type DeviceType uint8

DeviceType represents the type of compute device.

const (
	// CPU represents a CPU device.
	CPU DeviceType = iota
	// CUDA represents an NVIDIA GPU device.
	CUDA
)

func (DeviceType) String

func (d DeviceType) String() string

String returns the string representation of the device type.

type Div

type Div struct {
	// contains filtered or unexported fields
}

Div is a chainable operator that performs element-wise division.

func NewDiv

func NewDiv(backend BinaryOps, other *Tensor) *Div

NewDiv creates a Div operator.

func (*Div) Close

func (d *Div) Close() error

Close releases any resources held by this operator.

func (*Div) Identity

func (d *Div) Identity() pipz.Identity

Identity returns the operator identity.

func (*Div) Process

func (d *Div) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process performs element-wise division.

func (*Div) Schema

func (d *Div) Schema() pipz.Node

Schema returns the operator schema.

type Dropout

type Dropout struct {
	// contains filtered or unexported fields
}

Dropout is a chainable operator that applies dropout during training. During evaluation (inference mode), this is a no-op. p is the probability of dropping a value (0 to 1). Use WithTraining(ctx) to enable dropout during training.

func NewDropout

func NewDropout(backend ActivationOps, p float32) *Dropout

NewDropout creates a Dropout operator.

func (*Dropout) Close

func (d *Dropout) Close() error

Close releases any resources held by this operator.

func (*Dropout) Identity

func (d *Dropout) Identity() pipz.Identity

Identity returns the operator identity.

func (*Dropout) Process

func (d *Dropout) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies dropout to the input tensor.

func (*Dropout) Schema

func (d *Dropout) Schema() pipz.Node

Schema returns the operator schema.

type Embedding

type Embedding struct {
	// contains filtered or unexported fields
}

Embedding is a chainable operator that performs embedding lookup. Weight shape: [vocab_size, embed_dim] Input (indices) shape: arbitrary (e.g., [batch, seq_len]) Output shape: input.shape + [embed_dim]

The input tensor to Process should contain integer indices (stored as float32). Each index looks up the corresponding row in the weight matrix.

func NewEmbedding

func NewEmbedding(backend EmbeddingOps, weight *Tensor) *Embedding

NewEmbedding creates an Embedding operator. weight is the embedding table with shape [vocab_size, embed_dim].

func (*Embedding) Close

func (e *Embedding) Close() error

Close releases any resources held by this operator.

func (*Embedding) Identity

func (e *Embedding) Identity() pipz.Identity

Identity returns the operator identity.

func (*Embedding) Process

func (e *Embedding) Process(ctx context.Context, indices *Tensor) (*Tensor, error)

Process performs embedding lookup. The input tensor contains indices to look up in the weight matrix.

func (*Embedding) Schema

func (e *Embedding) Schema() pipz.Node

Schema returns the operator schema node.

type EmbeddingOps

type EmbeddingOps interface {
	// Embedding performs embedding lookup.
	// weight shape: [vocab_size, embed_dim]
	// indices shape: arbitrary (e.g., [batch, seq_len])
	// output shape: indices.shape + [embed_dim]
	Embedding(ctx context.Context, weight, indices *Tensor) (*Tensor, error)
}

EmbeddingOps defines embedding operations.

type ErrNotImplemented

type ErrNotImplemented struct {
	Op      string
	Backend string
}

ErrNotImplemented is returned when a backend doesn't support an operation.

func (*ErrNotImplemented) Error

func (e *ErrNotImplemented) Error() string

type Exp

type Exp struct {
	// contains filtered or unexported fields
}

Exp is a chainable operator that computes e^x for each element.

func NewExp

func NewExp(backend UnaryOps) *Exp

NewExp creates an Exp operator.

func (*Exp) Close

func (e *Exp) Close() error

Close releases any resources held by this operator.

func (*Exp) Identity

func (e *Exp) Identity() pipz.Identity

Identity returns the operator identity.

func (*Exp) Process

func (e *Exp) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes e^x for each element.

func (*Exp) Schema

func (e *Exp) Schema() pipz.Node

Schema returns the operator schema.

type Expand

type Expand struct {
	// contains filtered or unexported fields
}

Expand is a chainable operator that expands singleton dimensions. The tensor is not copied; stride is set to 0 for expanded dimensions. Backend-agnostic.

func NewExpand

func NewExpand(shape ...int) *Expand

NewExpand creates an Expand operator.

func (*Expand) Close

func (e *Expand) Close() error

Close releases any resources held by this operator.

func (*Expand) Identity

func (e *Expand) Identity() pipz.Identity

Identity returns the operator identity.

func (*Expand) Process

func (e *Expand) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process expands the tensor.

func (*Expand) Schema

func (e *Expand) Schema() pipz.Node

Schema returns the operator schema.

type Filler

type Filler interface {
	Fill(value float32) error
}

Filler is an interface for storage types that support filling with a value.

type Flatten

type Flatten struct {
	// contains filtered or unexported fields
}

Flatten is a chainable operator that flattens dimensions from startDim to endDim. Backend-agnostic.

func NewFlatten

func NewFlatten(startDim, endDim int) *Flatten

NewFlatten creates a Flatten operator.

func (*Flatten) Close

func (f *Flatten) Close() error

Close releases any resources held by this operator.

func (*Flatten) Identity

func (f *Flatten) Identity() pipz.Identity

Identity returns the operator identity.

func (*Flatten) Process

func (f *Flatten) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process flattens the tensor.

func (*Flatten) Schema

func (f *Flatten) Schema() pipz.Node

Schema returns the operator schema.

type GELU

type GELU struct {
	// contains filtered or unexported fields
}

GELU is a chainable operator that applies the GELU activation function. GELU(x) = x * 0.5 * (1 + erf(x / sqrt(2))).

func NewGELU

func NewGELU(backend UnaryOps) *GELU

NewGELU creates a GELU operator.

func (*GELU) Close

func (g *GELU) Close() error

Close releases any resources held by this operator.

func (*GELU) Identity

func (g *GELU) Identity() pipz.Identity

Identity returns the operator identity.

func (*GELU) Process

func (g *GELU) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies GELU to the input tensor.

func (*GELU) Schema

func (g *GELU) Schema() pipz.Node

Schema returns the operator schema.

type GroupNorm

type GroupNorm struct {
	// contains filtered or unexported fields
}

GroupNorm is a chainable operator that applies group normalization. Divides channels into groups and normalizes within each group. Input shape: [N, C, ...] where C must be divisible by numGroups.

func NewGroupNorm

func NewGroupNorm(backend NormOps, numGroups int, weight, bias *Tensor, epsilon float32) *GroupNorm

NewGroupNorm creates a GroupNorm operator.

func (*GroupNorm) Close

func (g *GroupNorm) Close() error

Close releases any resources held by this operator.

func (*GroupNorm) Identity

func (g *GroupNorm) Identity() pipz.Identity

Identity returns the operator identity.

func (*GroupNorm) Process

func (g *GroupNorm) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies group normalization.

func (*GroupNorm) Schema

func (g *GroupNorm) Schema() pipz.Node

Schema returns the operator schema node.

type HostCopier

type HostCopier interface {
	CopyToHost() ([]float32, error)
}

HostCopier is an interface for storage types that can copy data to host memory.

type InstanceNorm2d

type InstanceNorm2d struct {
	// contains filtered or unexported fields
}

InstanceNorm2d is a chainable operator that applies instance normalization over 4D input. Input shape: [N, C, H, W] Normalizes over H, W dimensions independently for each channel and sample.

func NewInstanceNorm2d

func NewInstanceNorm2d(backend NormOps, weight, bias *Tensor, epsilon float32) *InstanceNorm2d

NewInstanceNorm2d creates an InstanceNorm2d operator.

func (*InstanceNorm2d) Close

func (i *InstanceNorm2d) Close() error

Close releases any resources held by this operator.

func (*InstanceNorm2d) Identity

func (i *InstanceNorm2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*InstanceNorm2d) Process

func (i *InstanceNorm2d) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies instance normalization.

func (*InstanceNorm2d) Schema

func (i *InstanceNorm2d) Schema() pipz.Node

Schema returns the operator schema node.

type Int64HostCopier

type Int64HostCopier interface {
	CopyToHostInt64() ([]int64, error)
}

Int64HostCopier is an interface for storage types that can copy int64 data to host memory.

type L1Loss

type L1Loss struct {
	// contains filtered or unexported fields
}

L1Loss is a chainable operator that computes L1 (mean absolute error) loss. L1 = (1/n) * sum(|input - target|) for reduction="mean".

func NewL1Loss

func NewL1Loss(backend LossOps, target *Tensor, reduction string) *L1Loss

NewL1Loss creates an L1Loss operator.

func (*L1Loss) Close

func (l *L1Loss) Close() error

Close releases any resources held by this operator.

func (*L1Loss) Identity

func (l *L1Loss) Identity() pipz.Identity

Identity returns the operator identity.

func (*L1Loss) Process

func (l *L1Loss) Process(ctx context.Context, input *Tensor) (*Tensor, error)

Process computes L1 loss.

func (*L1Loss) Schema

func (l *L1Loss) Schema() pipz.Node

Schema returns the operator schema.

type LayerNorm

type LayerNorm struct {
	// contains filtered or unexported fields
}

LayerNorm is a chainable operator that applies layer normalization. Normalizes over the last len(normalizedShape) dimensions.

func NewLayerNorm

func NewLayerNorm(backend NormOps, normalizedShape []int, weight, bias *Tensor, epsilon float32) *LayerNorm

NewLayerNorm creates a LayerNorm operator.

func (*LayerNorm) Close

func (l *LayerNorm) Close() error

Close releases any resources held by this operator.

func (*LayerNorm) Identity

func (l *LayerNorm) Identity() pipz.Identity

Identity returns the operator identity.

func (*LayerNorm) Process

func (l *LayerNorm) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies layer normalization.

func (*LayerNorm) Schema

func (l *LayerNorm) Schema() pipz.Node

Schema returns the operator schema node.

type LeakyReLU

type LeakyReLU struct {
	// contains filtered or unexported fields
}

LeakyReLU is a chainable operator that applies leaky ReLU. LeakyReLU(x) = x if x > 0, else negative_slope * x.

func NewLeakyReLU

func NewLeakyReLU(backend ActivationOps, negativeSlope float32) *LeakyReLU

NewLeakyReLU creates a LeakyReLU operator.

func (*LeakyReLU) Close

func (l *LeakyReLU) Close() error

Close releases any resources held by this operator.

func (*LeakyReLU) Identity

func (l *LeakyReLU) Identity() pipz.Identity

Identity returns the operator identity.

func (*LeakyReLU) Process

func (l *LeakyReLU) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies leaky ReLU to the input tensor.

func (*LeakyReLU) Schema

func (l *LeakyReLU) Schema() pipz.Node

Schema returns the operator schema.

type Log

type Log struct {
	// contains filtered or unexported fields
}

Log is a chainable operator that computes the natural logarithm of each element.

func NewLog

func NewLog(backend UnaryOps) *Log

NewLog creates a Log operator.

func (*Log) Close

func (l *Log) Close() error

Close releases any resources held by this operator.

func (*Log) Identity

func (l *Log) Identity() pipz.Identity

Identity returns the operator identity.

func (*Log) Process

func (l *Log) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the natural logarithm of each element.

func (*Log) Schema

func (l *Log) Schema() pipz.Node

Schema returns the operator schema.

type LogSoftmax

type LogSoftmax struct {
	// contains filtered or unexported fields
}

LogSoftmax is a chainable operator that applies log softmax along a dimension. LogSoftmax(x)_i = log(exp(x_i) / sum(exp(x_j))) = x_i - log(sum(exp(x_j))).

func NewLogSoftmax

func NewLogSoftmax(backend ActivationOps, dim int) *LogSoftmax

NewLogSoftmax creates a LogSoftmax operator.

func (*LogSoftmax) Close

func (l *LogSoftmax) Close() error

Close releases any resources held by this operator.

func (*LogSoftmax) Identity

func (l *LogSoftmax) Identity() pipz.Identity

Identity returns the operator identity.

func (*LogSoftmax) Process

func (l *LogSoftmax) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies log softmax to the input tensor.

func (*LogSoftmax) Schema

func (l *LogSoftmax) Schema() pipz.Node

Schema returns the operator schema.

type LossOps

type LossOps interface {
	MSELoss(ctx context.Context, input, target *Tensor, reduction string) (*Tensor, error)
	L1Loss(ctx context.Context, input, target *Tensor, reduction string) (*Tensor, error)
	CrossEntropyLoss(ctx context.Context, input, target *Tensor, reduction string) (*Tensor, error)
	NLLLoss(ctx context.Context, input, target *Tensor, reduction string) (*Tensor, error)
}

LossOps defines loss functions.

type MSELoss

type MSELoss struct {
	// contains filtered or unexported fields
}

MSELoss is a chainable operator that computes mean squared error loss. MSE = (1/n) * sum((input - target)^2) for reduction="mean".

func NewMSELoss

func NewMSELoss(backend LossOps, target *Tensor, reduction string) *MSELoss

NewMSELoss creates an MSELoss operator.

func (*MSELoss) Close

func (m *MSELoss) Close() error

Close releases any resources held by this operator.

func (*MSELoss) Identity

func (m *MSELoss) Identity() pipz.Identity

Identity returns the operator identity.

func (*MSELoss) Process

func (m *MSELoss) Process(ctx context.Context, input *Tensor) (*Tensor, error)

Process computes MSE loss.

func (*MSELoss) Schema

func (m *MSELoss) Schema() pipz.Node

Schema returns the operator schema.

type MatMul

type MatMul struct {
	// contains filtered or unexported fields
}

MatMul is a chainable operator that performs matrix multiplication. Supports 2D matrices and batched matrix multiplication.

func NewMatMul

func NewMatMul(backend MatrixOps, other *Tensor) *MatMul

NewMatMul creates a MatMul operator.

func (*MatMul) Close

func (m *MatMul) Close() error

Close releases any resources held by this operator.

func (*MatMul) Identity

func (m *MatMul) Identity() pipz.Identity

Identity returns the operator identity.

func (*MatMul) Process

func (m *MatMul) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process performs matrix multiplication.

func (*MatMul) Schema

func (m *MatMul) Schema() pipz.Node

Schema returns the operator schema node.

type MatrixBackend

type MatrixBackend interface {
	MatrixOps
}

MatrixBackend provides matrix operations.

type MatrixOps

type MatrixOps interface {
	MatMul(ctx context.Context, a, b *Tensor) (*Tensor, error)
}

MatrixOps defines matrix operations.

type Max

type Max struct {
	// contains filtered or unexported fields
}

Max is a chainable operator that computes the maximum along dimensions. If no dimensions are specified, computes max of all elements.

func NewMax

func NewMax(backend ReduceOps, keepdim bool, dims ...int) *Max

NewMax creates a Max operator.

func (*Max) Close

func (m *Max) Close() error

Close releases any resources held by this operator.

func (*Max) Identity

func (m *Max) Identity() pipz.Identity

Identity returns the operator identity.

func (*Max) Process

func (m *Max) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the maximum along the specified dimensions.

func (*Max) Schema

func (m *Max) Schema() pipz.Node

Schema returns the operator schema.

type MaxPool2d

type MaxPool2d struct {
	// contains filtered or unexported fields
}

MaxPool2d is a chainable operator that applies 2D max pooling. Input shape: [N, C, H, W] or [C, H, W] Output shape: [N, C, H', W'] or [C, H', W'] where:

H' = (H + 2*padding[0] - kernel[0]) / stride[0] + 1
W' = (W + 2*padding[1] - kernel[1]) / stride[1] + 1

func NewMaxPool2d

func NewMaxPool2d(backend PoolOps, config Pool2dConfig) *MaxPool2d

NewMaxPool2d creates a MaxPool2d operator.

func (*MaxPool2d) Close

func (m *MaxPool2d) Close() error

Close releases any resources held by this operator.

func (*MaxPool2d) Identity

func (m *MaxPool2d) Identity() pipz.Identity

Identity returns the operator identity.

func (*MaxPool2d) Process

func (m *MaxPool2d) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies 2D max pooling.

func (*MaxPool2d) Schema

func (m *MaxPool2d) Schema() pipz.Node

Schema returns the operator schema node.

type Mean

type Mean struct {
	// contains filtered or unexported fields
}

Mean is a chainable operator that computes the mean along dimensions. If no dimensions are specified, computes mean of all elements.

func NewMean

func NewMean(backend ReduceOps, keepdim bool, dims ...int) *Mean

NewMean creates a Mean operator.

func (*Mean) Close

func (m *Mean) Close() error

Close releases any resources held by this operator.

func (*Mean) Identity

func (m *Mean) Identity() pipz.Identity

Identity returns the operator identity.

func (*Mean) Process

func (m *Mean) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the mean along the specified dimensions.

func (*Mean) Schema

func (m *Mean) Schema() pipz.Node

Schema returns the operator schema.

type MemoryAllocator

type MemoryAllocator interface {
	// Malloc allocates memory of the given size in bytes.
	Malloc(bytes int) (uintptr, error)
	// Free releases previously allocated memory.
	Free(ptr uintptr) error
}

MemoryAllocator is the interface for backend-specific memory allocation. This is used by the memory pool for device memory management.

func GetMemoryAllocator

func GetMemoryAllocator(dt DeviceType) (MemoryAllocator, bool)

GetMemoryAllocator returns the memory allocator for a device type.

type Min

type Min struct {
	// contains filtered or unexported fields
}

Min is a chainable operator that computes the minimum along dimensions. If no dimensions are specified, computes min of all elements.

func NewMin

func NewMin(backend ReduceOps, keepdim bool, dims ...int) *Min

NewMin creates a Min operator.

func (*Min) Close

func (m *Min) Close() error

Close releases any resources held by this operator.

func (*Min) Identity

func (m *Min) Identity() pipz.Identity

Identity returns the operator identity.

func (*Min) Process

func (m *Min) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the minimum along the specified dimensions.

func (*Min) Schema

func (m *Min) Schema() pipz.Node

Schema returns the operator schema.

type MockStorage

type MockStorage struct {
	// contains filtered or unexported fields
}

MockStorage is a minimal storage implementation for testing. It tracks metadata but does not allocate real memory. Operations are no-ops; use this for testing shape logic and pipz integration.

func NewMockStorage

func NewMockStorage(numel int, dtype DType, device Device) *MockStorage

NewMockStorage creates a mock storage with the given parameters.

func (*MockStorage) Clone

func (s *MockStorage) Clone() Storage

Clone creates a copy of the mock storage.

func (*MockStorage) DType

func (s *MockStorage) DType() DType

DType returns the data type.

func (*MockStorage) Device

func (s *MockStorage) Device() Device

Device returns the mock device.

func (*MockStorage) Free

func (s *MockStorage) Free()

Free is a no-op for mock storage.

func (*MockStorage) Len

func (s *MockStorage) Len() int

Len returns the number of elements.

func (*MockStorage) Ptr

func (s *MockStorage) Ptr() uintptr

Ptr returns 0 (no real memory).

func (*MockStorage) Size

func (s *MockStorage) Size() int

Size returns the theoretical size in bytes.

type Mul

type Mul struct {
	// contains filtered or unexported fields
}

Mul is a chainable operator that performs element-wise multiplication.

func NewMul

func NewMul(backend BinaryOps, other *Tensor) *Mul

NewMul creates a Mul operator.

func (*Mul) Close

func (m *Mul) Close() error

Close releases any resources held by this operator.

func (*Mul) Identity

func (m *Mul) Identity() pipz.Identity

Identity returns the operator identity.

func (*Mul) Process

func (m *Mul) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process performs element-wise multiplication.

func (*Mul) Schema

func (m *Mul) Schema() pipz.Node

Schema returns the operator schema.

type NLLLoss

type NLLLoss struct {
	// contains filtered or unexported fields
}

NLLLoss is a chainable operator that computes negative log likelihood loss. Input: [N, C] log-probabilities (already log-softmaxed) Target: [N] class indices (integers as float32) Loss = -input[target].

func NewNLLLoss

func NewNLLLoss(backend LossOps, target *Tensor, reduction string) *NLLLoss

NewNLLLoss creates an NLLLoss operator.

func (*NLLLoss) Close

func (n *NLLLoss) Close() error

Close releases any resources held by this operator.

func (*NLLLoss) Identity

func (n *NLLLoss) Identity() pipz.Identity

Identity returns the operator identity.

func (*NLLLoss) Process

func (n *NLLLoss) Process(ctx context.Context, input *Tensor) (*Tensor, error)

Process computes NLL loss.

func (*NLLLoss) Schema

func (n *NLLLoss) Schema() pipz.Node

Schema returns the operator schema.

type Narrow

type Narrow struct {
	// contains filtered or unexported fields
}

Narrow is an alias for Slice. Backend-agnostic.

func NewNarrow

func NewNarrow(dim, start, length int) *Narrow

NewNarrow creates a Narrow operator.

func (*Narrow) Close

func (n *Narrow) Close() error

Close releases any resources held by this operator.

func (*Narrow) Identity

func (n *Narrow) Identity() pipz.Identity

Identity returns the operator identity.

func (*Narrow) Process

func (n *Narrow) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process narrows the tensor.

func (*Narrow) Schema

func (n *Narrow) Schema() pipz.Node

Schema returns the operator schema.

type Neg

type Neg struct {
	// contains filtered or unexported fields
}

Neg is a chainable operator that negates each element.

func NewNeg

func NewNeg(backend UnaryOps) *Neg

NewNeg creates a Neg operator.

func (*Neg) Close

func (n *Neg) Close() error

Close releases any resources held by this operator.

func (*Neg) Identity

func (n *Neg) Identity() pipz.Identity

Identity returns the operator identity.

func (*Neg) Process

func (n *Neg) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process negates each element.

func (*Neg) Schema

func (n *Neg) Schema() pipz.Node

Schema returns the operator schema.

type NeuralBackend

type NeuralBackend interface {
	ActivationOps
	NormOps
	ConvOps
	PoolOps
	LossOps
	EmbeddingOps
	ShapeOps
}

NeuralBackend provides neural network operations.

type NormOps

type NormOps interface {
	BatchNorm2d(ctx context.Context, input, weight, bias, runningMean, runningVar *Tensor, epsilon, momentum float32, training bool) (*Tensor, error)
	LayerNorm(ctx context.Context, input *Tensor, normalizedShape []int, weight, bias *Tensor, epsilon float32) (*Tensor, error)
	RMSNorm(ctx context.Context, input *Tensor, normalizedShape []int, weight *Tensor, epsilon float32) (*Tensor, error)
	GroupNorm(ctx context.Context, input *Tensor, numGroups int, weight, bias *Tensor, epsilon float32) (*Tensor, error)
	InstanceNorm2d(ctx context.Context, input, weight, bias *Tensor, epsilon float32) (*Tensor, error)
}

NormOps defines normalization operations.

type Permute

type Permute struct {
	// contains filtered or unexported fields
}

Permute is a chainable operator that permutes dimensions. Backend-agnostic.

func NewPermute

func NewPermute(dims ...int) *Permute

NewPermute creates a Permute operator.

func (*Permute) Close

func (p *Permute) Close() error

Close releases any resources held by this operator.

func (*Permute) Identity

func (p *Permute) Identity() pipz.Identity

Identity returns the operator identity.

func (*Permute) Process

func (p *Permute) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process permutes the tensor dimensions.

func (*Permute) Schema

func (p *Permute) Schema() pipz.Node

Schema returns the operator schema.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool manages memory allocation and reuse for tensor storage. It maintains separate pools for CPU and each CUDA device.

func DefaultPool

func DefaultPool() *Pool

DefaultPool returns the default memory pool.

func NewPool

func NewPool() *Pool

NewPool creates a new memory pool.

func PoolFromContext

func PoolFromContext(ctx context.Context) *Pool

PoolFromContext returns the memory pool from the context. Returns the default pool if no pool is set in the context.

func (*Pool) AllocCPU

func (p *Pool) AllocCPU(numel int, dtype DType) *CPUStorage

AllocCPU allocates or retrieves a CPU storage of the given size.

func (*Pool) AllocCUDA

func (p *Pool) AllocCUDA(numel int, dtype DType, device int) (uintptr, error)

AllocCUDA allocates or retrieves CUDA memory of the given size. Returns the device pointer and an error if CUDA is unavailable.

func (*Pool) Clear

func (p *Pool) Clear()

Clear releases all cached memory back to the system.

func (*Pool) FreeCPU

func (p *Pool) FreeCPU(storage *CPUStorage)

FreeCPU returns a CPU storage to the pool for reuse.

func (*Pool) FreeCUDA

func (p *Pool) FreeCUDA(ptr uintptr, numel int, dtype DType, device int)

FreeCUDA returns CUDA memory to the pool for reuse.

func (*Pool) MaxCacheSize

func (p *Pool) MaxCacheSize() int64

MaxCacheSize returns the current maximum cache size (0 = unlimited).

func (*Pool) SetContext

func (p *Pool) SetContext(ctx context.Context)

SetContext sets the context used for emitting pool events.

func (*Pool) SetMaxCacheSize

func (p *Pool) SetMaxCacheSize(bytes int64)

SetMaxCacheSize sets the maximum bytes to cache before eviction. Set to 0 for unlimited (default). When the cache exceeds this limit, oldest blocks are evicted until under the limit.

func (*Pool) Stats

func (p *Pool) Stats() PoolStats

Stats returns current pool statistics.

type Pool2dConfig

type Pool2dConfig struct {
	KernelSize [2]int // [height, width]
	Stride     [2]int // [height, width] - defaults to KernelSize if zero
	Padding    [2]int // [height, width]
}

Pool2dConfig configures 2D pooling operations.

type PoolAllocator

type PoolAllocator interface {
	// Alloc allocates storage of the given size and dtype.
	Alloc(numel int, dtype DType) (Storage, error)

	// Free returns storage to the pool.
	Free(s Storage)

	// Clear releases all cached memory.
	Clear()
}

PoolAllocator is the interface for backend-specific pool allocation. Each backend (CPU, CUDA, etc.) implements this interface to provide memory pooling for its device type.

type PoolOps

type PoolOps interface {
	MaxPool2d(ctx context.Context, input *Tensor, kernelSize, stride, padding [2]int) (*Tensor, error)
	AvgPool2d(ctx context.Context, input *Tensor, kernelSize, stride, padding [2]int) (*Tensor, error)
	AdaptiveAvgPool2d(ctx context.Context, input *Tensor, outputSize [2]int) (*Tensor, error)
	AdaptiveMaxPool2d(ctx context.Context, input *Tensor, outputSize [2]int) (*Tensor, error)
}

PoolOps defines pooling operations.

type PoolStats

type PoolStats struct {
	CUDAAllocations   map[int]int64
	CUDADeallocations map[int]int64
	CUDABytesInUse    map[int]int64
	CUDABytesCached   map[int]int64
	CPUAllocations    int64
	CPUDeallocations  int64
	CPUBytesInUse     int64
	CPUBytesCached    int64
}

PoolStats tracks memory pool statistics.

type PoolableStorage

type PoolableStorage interface {
	Storage
	// PoolKey returns the metadata needed for pool free operations.
	PoolKey() (numel int, dtype DType, deviceIndex int)
}

PoolableStorage extends Storage with metadata needed for memory pool operations. Storage implementations that support pooling should implement this interface.

type Pow

type Pow struct {
	// contains filtered or unexported fields
}

Pow is a chainable operator that raises each element to a power.

func NewPow

func NewPow(backend BinaryOps, exp float32) *Pow

NewPow creates a Pow operator.

func (*Pow) Close

func (p *Pow) Close() error

Close releases any resources held by this operator.

func (*Pow) Identity

func (p *Pow) Identity() pipz.Identity

Identity returns the operator identity.

func (*Pow) Process

func (p *Pow) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process raises each element to the power.

func (*Pow) Schema

func (p *Pow) Schema() pipz.Node

Schema returns the operator schema.

type Prod

type Prod struct {
	// contains filtered or unexported fields
}

Prod is a chainable operator that computes the product along dimensions. If no dimensions are specified, computes product of all elements.

func NewProd

func NewProd(backend ReduceOps, keepdim bool, dims ...int) *Prod

NewProd creates a Prod operator.

func (*Prod) Close

func (p *Prod) Close() error

Close releases any resources held by this operator.

func (*Prod) Identity

func (p *Prod) Identity() pipz.Identity

Identity returns the operator identity.

func (*Prod) Process

func (p *Prod) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the product along the specified dimensions.

func (*Prod) Schema

func (p *Prod) Schema() pipz.Node

Schema returns the operator schema.

type RMSNorm

type RMSNorm struct {
	// contains filtered or unexported fields
}

RMSNorm is a chainable operator that applies RMS (Root Mean Square) normalization. Normalizes over the last len(normalizedShape) dimensions without mean subtraction.

func NewRMSNorm

func NewRMSNorm(backend NormOps, normalizedShape []int, weight *Tensor, epsilon float32) *RMSNorm

NewRMSNorm creates an RMSNorm operator.

func (*RMSNorm) Close

func (r *RMSNorm) Close() error

Close releases any resources held by this operator.

func (*RMSNorm) Identity

func (r *RMSNorm) Identity() pipz.Identity

Identity returns the operator identity.

func (*RMSNorm) Process

func (r *RMSNorm) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies RMS normalization.

func (*RMSNorm) Schema

func (r *RMSNorm) Schema() pipz.Node

Schema returns the operator schema node.

type ReLU

type ReLU struct {
	// contains filtered or unexported fields
}

ReLU is a chainable operator that applies the ReLU activation function. ReLU(x) = max(0, x).

func NewReLU

func NewReLU(backend UnaryOps) *ReLU

NewReLU creates a ReLU operator.

func (*ReLU) Close

func (r *ReLU) Close() error

Close releases any resources held by this operator.

func (*ReLU) Identity

func (r *ReLU) Identity() pipz.Identity

Identity returns the operator identity.

func (*ReLU) Process

func (r *ReLU) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies ReLU to the input tensor.

func (*ReLU) Schema

func (r *ReLU) Schema() pipz.Node

Schema returns the operator schema.

type ReduceBackend

type ReduceBackend interface {
	ReduceOps
}

ReduceBackend provides reduction operations.

type ReduceOps

type ReduceOps interface {
	Sum(ctx context.Context, t *Tensor, dims []int, keepdim bool) (*Tensor, error)
	Mean(ctx context.Context, t *Tensor, dims []int, keepdim bool) (*Tensor, error)
	Max(ctx context.Context, t *Tensor, dims []int, keepdim bool) (*Tensor, error)
	Min(ctx context.Context, t *Tensor, dims []int, keepdim bool) (*Tensor, error)
	Var(ctx context.Context, t *Tensor, dims []int, keepdim bool, correction int) (*Tensor, error)
	Std(ctx context.Context, t *Tensor, dims []int, keepdim bool, correction int) (*Tensor, error)
	Prod(ctx context.Context, t *Tensor, dims []int, keepdim bool) (*Tensor, error)
	ArgMax(ctx context.Context, t *Tensor, dim int, keepdim bool) (*Tensor, error)
	ArgMin(ctx context.Context, t *Tensor, dim int, keepdim bool) (*Tensor, error)
}

ReduceOps defines reduction operations.

type Reshape

type Reshape struct {
	// contains filtered or unexported fields
}

Reshape is a chainable operator that reshapes a tensor. The total number of elements must remain the same. A single dimension can be -1, which will be inferred. Backend-agnostic: creates a view or copies if non-contiguous.

func NewReshape

func NewReshape(shape ...int) *Reshape

NewReshape creates a Reshape operator.

func (*Reshape) Close

func (r *Reshape) Close() error

Close releases any resources held by this operator.

func (*Reshape) Identity

func (r *Reshape) Identity() pipz.Identity

Identity returns the operator identity.

func (*Reshape) Process

func (r *Reshape) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process reshapes the tensor.

func (*Reshape) Schema

func (r *Reshape) Schema() pipz.Node

Schema returns the operator schema.

type SafeTensorInfo

type SafeTensorInfo struct {
	Name  string
	DType DType
	Shape []int
}

SafeTensorInfo contains metadata about a tensor in a SafeTensors file.

type SafeTensorsFile

type SafeTensorsFile struct {
	// contains filtered or unexported fields
}

SafeTensorsFile represents an opened SafeTensors file.

func OpenSafeTensors

func OpenSafeTensors(path string) (*SafeTensorsFile, error)

OpenSafeTensors opens a SafeTensors file for reading.

func (*SafeTensorsFile) Close

func (f *SafeTensorsFile) Close() error

Close closes the file.

func (*SafeTensorsFile) Load

func (f *SafeTensorsFile) Load(name string, backend StorageBackend, targetDType *DType) (*Tensor, error)

Load reads a tensor from the file and creates a tendo Tensor using the provided backend. If targetDType is nil, the native file dtype is preserved. If targetDType is specified, the storage will use that dtype marker.

func (*SafeTensorsFile) LoadCPU

func (f *SafeTensorsFile) LoadCPU(name string, targetDType *DType) (*Tensor, error)

LoadCPU is a convenience method that loads a tensor to CPU storage. If targetDType is nil, the native file dtype is preserved.

func (*SafeTensorsFile) NumTensors

func (f *SafeTensorsFile) NumTensors() int

NumTensors returns the number of tensors in the file.

func (*SafeTensorsFile) Path

func (f *SafeTensorsFile) Path() string

Path returns the file path.

func (*SafeTensorsFile) TensorInfo

func (f *SafeTensorsFile) TensorInfo(name string) *SafeTensorInfo

TensorInfo returns metadata for a specific tensor, or nil if not found or if the dtype is unsupported.

func (*SafeTensorsFile) Tensors

func (f *SafeTensorsFile) Tensors() map[string]SafeTensorInfo

Tensors returns metadata for all tensors in the file. Tensors with unsupported dtypes are omitted from the result.

type ShapeError

type ShapeError struct {
	Op      string
	Message string
	ShapeA  []int
	ShapeB  []int
}

ShapeError indicates a shape-related error.

func (*ShapeError) Error

func (e *ShapeError) Error() string

func (*ShapeError) Is

func (e *ShapeError) Is(target error) bool

Is reports whether target is a ShapeError.

type ShapeOps

type ShapeOps interface {
	// Cat concatenates tensors along an existing dimension.
	Cat(ctx context.Context, tensors []*Tensor, dim int) (*Tensor, error)

	// Stack stacks tensors along a new dimension.
	Stack(ctx context.Context, tensors []*Tensor, dim int) (*Tensor, error)
}

ShapeOps defines tensor shape manipulation operations that require data movement.

type SiLU

type SiLU struct {
	// contains filtered or unexported fields
}

SiLU is a chainable operator that applies the SiLU (Swish) activation. SiLU(x) = x * sigmoid(x).

func NewSiLU

func NewSiLU(backend UnaryOps) *SiLU

NewSiLU creates a SiLU operator.

func (*SiLU) Close

func (s *SiLU) Close() error

Close releases any resources held by this operator.

func (*SiLU) Identity

func (s *SiLU) Identity() pipz.Identity

Identity returns the operator identity.

func (*SiLU) Process

func (s *SiLU) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies SiLU to the input tensor.

func (*SiLU) Schema

func (s *SiLU) Schema() pipz.Node

Schema returns the operator schema.

type Sigmoid

type Sigmoid struct {
	// contains filtered or unexported fields
}

Sigmoid is a chainable operator that applies the sigmoid activation function. Sigmoid(x) = 1 / (1 + exp(-x)).

func NewSigmoid

func NewSigmoid(backend UnaryOps) *Sigmoid

NewSigmoid creates a Sigmoid operator.

func (*Sigmoid) Close

func (s *Sigmoid) Close() error

Close releases any resources held by this operator.

func (*Sigmoid) Identity

func (s *Sigmoid) Identity() pipz.Identity

Identity returns the operator identity.

func (*Sigmoid) Process

func (s *Sigmoid) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies sigmoid to the input tensor.

func (*Sigmoid) Schema

func (s *Sigmoid) Schema() pipz.Node

Schema returns the operator schema.

type Sign

type Sign struct {
	// contains filtered or unexported fields
}

Sign is a chainable operator that computes the sign of each element. Returns -1 for negative, 0 for zero, 1 for positive.

func NewSign

func NewSign(backend UnaryOps) *Sign

NewSign creates a Sign operator.

func (*Sign) Close

func (s *Sign) Close() error

Close releases any resources held by this operator.

func (*Sign) Identity

func (s *Sign) Identity() pipz.Identity

Identity returns the operator identity.

func (*Sign) Process

func (s *Sign) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the sign of each element.

func (*Sign) Schema

func (s *Sign) Schema() pipz.Node

Schema returns the operator schema.

type Sin

type Sin struct {
	// contains filtered or unexported fields
}

Sin is a chainable operator that computes the sine of each element.

func NewSin

func NewSin(backend UnaryOps) *Sin

NewSin creates a Sin operator.

func (*Sin) Close

func (s *Sin) Close() error

Close releases any resources held by this operator.

func (*Sin) Identity

func (s *Sin) Identity() pipz.Identity

Identity returns the operator identity.

func (*Sin) Process

func (s *Sin) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the sine of each element.

func (*Sin) Schema

func (s *Sin) Schema() pipz.Node

Schema returns the operator schema.

type Slice

type Slice struct {
	// contains filtered or unexported fields
}

Slice is a chainable operator that extracts a slice along a dimension. start is inclusive, end is exclusive. Backend-agnostic.

func NewSlice

func NewSlice(dim, start, end int) *Slice

NewSlice creates a Slice operator.

func (*Slice) Close

func (sl *Slice) Close() error

Close releases any resources held by this operator.

func (*Slice) Identity

func (sl *Slice) Identity() pipz.Identity

Identity returns the operator identity.

func (*Slice) Process

func (sl *Slice) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process slices the tensor.

func (*Slice) Schema

func (sl *Slice) Schema() pipz.Node

Schema returns the operator schema.

type Softmax

type Softmax struct {
	// contains filtered or unexported fields
}

Softmax is a chainable operator that applies softmax along a dimension. Softmax(x)_i = exp(x_i) / sum(exp(x_j)).

func NewSoftmax

func NewSoftmax(backend ActivationOps, dim int) *Softmax

NewSoftmax creates a Softmax operator.

func (*Softmax) Close

func (s *Softmax) Close() error

Close releases any resources held by this operator.

func (*Softmax) Identity

func (s *Softmax) Identity() pipz.Identity

Identity returns the operator identity.

func (*Softmax) Process

func (s *Softmax) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process applies softmax to the input tensor.

func (*Softmax) Schema

func (s *Softmax) Schema() pipz.Node

Schema returns the operator schema.

type Sqrt

type Sqrt struct {
	// contains filtered or unexported fields
}

Sqrt is a chainable operator that computes the square root of each element.

func NewSqrt

func NewSqrt(backend UnaryOps) *Sqrt

NewSqrt creates a Sqrt operator.

func (*Sqrt) Close

func (s *Sqrt) Close() error

Close releases any resources held by this operator.

func (*Sqrt) Identity

func (s *Sqrt) Identity() pipz.Identity

Identity returns the operator identity.

func (*Sqrt) Process

func (s *Sqrt) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes the square root of each element.

func (*Sqrt) Schema

func (s *Sqrt) Schema() pipz.Node

Schema returns the operator schema.

type Square

type Square struct {
	// contains filtered or unexported fields
}

Square is a chainable operator that squares each element.

func NewSquare

func NewSquare(backend UnaryOps) *Square

NewSquare creates a Square operator.

func (*Square) Close

func (s *Square) Close() error

Close releases any resources held by this operator.

func (*Square) Identity

func (s *Square) Identity() pipz.Identity

Identity returns the operator identity.

func (*Square) Process

func (s *Square) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process squares each element.

func (*Square) Schema

func (s *Square) Schema() pipz.Node

Schema returns the operator schema.

type Squeeze

type Squeeze struct {
	// contains filtered or unexported fields
}

Squeeze is a chainable operator that removes dimensions of size 1. If dim is specified, only that dimension is squeezed (if size 1). Backend-agnostic.

func NewSqueeze

func NewSqueeze() *Squeeze

NewSqueeze creates a Squeeze operator that squeezes all size-1 dimensions.

func NewSqueezeDim

func NewSqueezeDim(dim int) *Squeeze

NewSqueezeDim creates a Squeeze operator that squeezes a specific dimension.

func (*Squeeze) Close

func (s *Squeeze) Close() error

Close releases any resources held by this operator.

func (*Squeeze) Identity

func (s *Squeeze) Identity() pipz.Identity

Identity returns the operator identity.

func (*Squeeze) Process

func (s *Squeeze) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process squeezes the tensor.

func (*Squeeze) Schema

func (s *Squeeze) Schema() pipz.Node

Schema returns the operator schema.

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is a chainable operator that stacks tensors along a new dimension. All tensors must have identical shapes.

func NewStack

func NewStack(backend ShapeOps, tensors []*Tensor, dim int) *Stack

NewStack creates a Stack operator. If backend is nil, uses CPU-only fallback implementation.

func (*Stack) Close

func (s *Stack) Close() error

Close releases any resources held by this operator.

func (*Stack) Identity

func (s *Stack) Identity() pipz.Identity

Identity returns the operator identity.

func (*Stack) Process

func (s *Stack) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process stacks tensors.

func (*Stack) Schema

func (s *Stack) Schema() pipz.Node

Schema returns the operator schema.

type Std

type Std struct {
	// contains filtered or unexported fields
}

Std is a chainable operator that computes standard deviation along dimensions. Uses Bessel's correction (N-1) by default for unbiased estimation.

func NewStd

func NewStd(backend ReduceOps, keepdim bool, correction int, dims ...int) *Std

NewStd creates a Std operator.

func (*Std) Close

func (s *Std) Close() error

Close releases any resources held by this operator.

func (*Std) Identity

func (s *Std) Identity() pipz.Identity

Identity returns the operator identity.

func (*Std) Process

func (s *Std) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes standard deviation along the specified dimensions.

func (*Std) Schema

func (s *Std) Schema() pipz.Node

Schema returns the operator schema.

type Storage

type Storage interface {
	// Ptr returns the raw pointer to the data (CPU or GPU address).
	Ptr() uintptr

	// Size returns the size in bytes of the allocated memory.
	Size() int

	// Len returns the number of elements in the storage.
	Len() int

	// Device returns the device where this storage resides.
	Device() Device

	// DType returns the data type of elements in this storage.
	DType() DType

	// Clone creates a deep copy of the storage.
	Clone() Storage

	// Free releases the underlying memory.
	// After calling Free, the storage should not be used.
	Free()
}

Storage is the interface for tensor data storage backends. Implementations handle memory allocation and device-specific operations.

type StorageBackend

type StorageBackend interface {
	DeviceType() DeviceType
	NewStorage(numel int, dtype DType, deviceIndex int) (Storage, error)
	NewStorageFromSlice(data []float32, dtype DType, deviceIndex int) (Storage, error)
}

StorageBackend is a minimal interface for storage creation. This is used during the transition period for backwards compatibility.

func GetBackend

func GetBackend(dt DeviceType) (StorageBackend, bool)

GetBackend returns a storage backend by device type.

type StorageOps

type StorageOps interface {
	// NewStorage creates storage on this backend's device.
	NewStorage(numel int, dtype DType, deviceIndex int) (Storage, error)

	// NewStorageFromSlice creates storage initialized from host data.
	NewStorageFromSlice(data []float32, dtype DType, deviceIndex int) (Storage, error)

	// CopyFrom copies a tensor from another device to this backend's device.
	// Returns a new tensor on this backend's device.
	CopyFrom(t *Tensor) (*Tensor, error)
}

StorageOps defines storage creation and device transfer operations.

type Sub

type Sub struct {
	// contains filtered or unexported fields
}

Sub is a chainable operator that performs element-wise subtraction.

func NewSub

func NewSub(backend BinaryOps, other *Tensor) *Sub

NewSub creates a Sub operator.

func (*Sub) Close

func (s *Sub) Close() error

Close releases any resources held by this operator.

func (*Sub) Identity

func (s *Sub) Identity() pipz.Identity

Identity returns the operator identity.

func (*Sub) Process

func (s *Sub) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process performs element-wise subtraction.

func (*Sub) Schema

func (s *Sub) Schema() pipz.Node

Schema returns the operator schema.

type Sum

type Sum struct {
	// contains filtered or unexported fields
}

Sum is a chainable operator that sums elements along dimensions. If no dimensions are specified, sums all elements.

func NewSum

func NewSum(backend ReduceOps, keepdim bool, dims ...int) *Sum

NewSum creates a Sum operator.

func (*Sum) Close

func (s *Sum) Close() error

Close releases any resources held by this operator.

func (*Sum) Identity

func (s *Sum) Identity() pipz.Identity

Identity returns the operator identity.

func (*Sum) Process

func (s *Sum) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process sums elements along the specified dimensions.

func (*Sum) Schema

func (s *Sum) Schema() pipz.Node

Schema returns the operator schema.

type Syncer

type Syncer interface {
	Sync() error
}

Syncer is an interface for storage types that support device synchronization.

type T

type T struct {
	// contains filtered or unexported fields
}

T is a chainable operator that transposes the last two dimensions. Equivalent to Transpose(-2, -1). Backend-agnostic.

func NewT

func NewT() *T

NewT creates a T operator.

func (*T) Close

func (t *T) Close() error

Close releases any resources held by this operator.

func (*T) Identity

func (t *T) Identity() pipz.Identity

Identity returns the operator identity.

func (*T) Process

func (t *T) Process(ctx context.Context, in *Tensor) (*Tensor, error)

Process transposes the last two dimensions.

func (*T) Schema

func (t *T) Schema() pipz.Node

Schema returns the operator schema node.

type Tanh

type Tanh struct {
	// contains filtered or unexported fields
}

Tanh is a chainable operator that applies the tanh activation function.

func NewTanh

func NewTanh(backend UnaryOps) *Tanh

NewTanh creates a Tanh operator.

func (*Tanh) Close

func (t *Tanh) Close() error

Close releases any resources held by this operator.

func (*Tanh) Identity

func (t *Tanh) Identity() pipz.Identity

Identity returns the operator identity.

func (*Tanh) Process

func (t *Tanh) Process(ctx context.Context, in *Tensor) (*Tensor, error)

Process applies tanh to the input tensor.

func (*Tanh) Schema

func (t *Tanh) Schema() pipz.Node

Schema returns the operator schema.

type Tensor

type Tensor struct {
	// contains filtered or unexported fields
}

Tensor represents a multi-dimensional array of numeric data. It wraps a Storage backend and maintains shape/stride metadata for interpreting the underlying data.

func Arange

func Arange(start, end, step float32) (*Tensor, error)

Arange creates a 1D CPU tensor with values from start to end (exclusive) with step.

func ArangeOn

func ArangeOn(backend Backend, start, end, step float32) (*Tensor, error)

ArangeOn creates a 1D tensor with values from start to end using the given backend.

func Empty

func Empty(shape ...int) (*Tensor, error)

Empty creates an uninitialized CPU tensor with the given shape.

func EmptyLike

func EmptyLike(t *Tensor) (*Tensor, error)

EmptyLike creates an uninitialized tensor with the same shape and dtype as the input. The tensor is created on CPU.

func EmptyLikeOn

func EmptyLikeOn(backend Backend, t *Tensor) (*Tensor, error)

EmptyLikeOn creates an uninitialized tensor with the same shape as the input using the given backend.

func EmptyOn

func EmptyOn(backend Backend, shape ...int) (*Tensor, error)

EmptyOn creates an uninitialized tensor using the given backend.

func Eye

func Eye(n int) (*Tensor, error)

Eye creates a 2D CPU identity matrix of size n x n.

func EyeOn

func EyeOn(backend Backend, n int) (*Tensor, error)

EyeOn creates a 2D identity matrix using the given backend.

func FromSlice

func FromSlice(data []float32, shape ...int) (*Tensor, error)

FromSlice creates a CPU tensor from a float32 slice with the given shape.

func FromSliceOn

func FromSliceOn(backend Backend, data []float32, shape ...int) (*Tensor, error)

FromSliceOn creates a tensor from a float32 slice using the given backend.

func Full

func Full(value float32, shape ...int) (*Tensor, error)

Full creates a CPU tensor filled with the given value.

func FullOn

func FullOn(backend Backend, value float32, shape ...int) (*Tensor, error)

FullOn creates a tensor filled with the given value using the given backend.

func Linspace

func Linspace(start, end float32, n int) (*Tensor, error)

Linspace creates a 1D CPU tensor with n evenly spaced values from start to end (inclusive).

func LinspaceOn

func LinspaceOn(backend Backend, start, end float32, n int) (*Tensor, error)

LinspaceOn creates a 1D tensor with evenly spaced values using the given backend.

func MustArange

func MustArange(start, end, step float32) *Tensor

MustArange creates a 1D CPU tensor with values from start to end (exclusive) with step. Panics on error.

func MustArangeOn

func MustArangeOn(backend Backend, start, end, step float32) *Tensor

MustArangeOn creates a 1D tensor with values from start to end using the given backend. Panics on error.

func MustEmpty

func MustEmpty(shape ...int) *Tensor

MustEmpty creates an uninitialized CPU tensor with the given shape. Panics on error.

func MustEmptyLike

func MustEmptyLike(t *Tensor) *Tensor

MustEmptyLike creates an uninitialized tensor with the same shape and dtype as the input. Panics on error.

func MustEmptyOn

func MustEmptyOn(backend Backend, shape ...int) *Tensor

MustEmptyOn creates an uninitialized tensor using the given backend. Panics on error.

func MustEye

func MustEye(n int) *Tensor

MustEye creates a 2D CPU identity matrix of size n x n. Panics on error.

func MustEyeOn

func MustEyeOn(backend Backend, n int) *Tensor

MustEyeOn creates a 2D identity matrix using the given backend. Panics on error.

func MustFromSlice

func MustFromSlice(data []float32, shape ...int) *Tensor

MustFromSlice creates a CPU tensor from a float32 slice with the given shape. Panics on error.

func MustFromSliceOn

func MustFromSliceOn(backend Backend, data []float32, shape ...int) *Tensor

MustFromSliceOn creates a tensor from a float32 slice using the given backend. Panics on error.

func MustFull

func MustFull(value float32, shape ...int) *Tensor

MustFull creates a CPU tensor filled with the given value. Panics on error.

func MustFullOn

func MustFullOn(backend Backend, value float32, shape ...int) *Tensor

MustFullOn creates a tensor filled with the given value using the given backend. Panics on error.

func MustLinspace

func MustLinspace(start, end float32, n int) *Tensor

MustLinspace creates a 1D CPU tensor with n evenly spaced values from start to end. Panics on error.

func MustLinspaceOn

func MustLinspaceOn(backend Backend, start, end float32, n int) *Tensor

MustLinspaceOn creates a 1D tensor with evenly spaced values using the given backend. Panics on error.

func MustOnes

func MustOnes(shape ...int) *Tensor

MustOnes creates a CPU tensor filled with ones. Panics on error.

func MustOnesLike

func MustOnesLike(t *Tensor) *Tensor

MustOnesLike creates a tensor of ones with the same shape and dtype as the input. Panics on error.

func MustOnesOn

func MustOnesOn(backend Backend, shape ...int) *Tensor

MustOnesOn creates a tensor filled with ones using the given backend. Panics on error.

func MustRand

func MustRand(shape ...int) *Tensor

MustRand creates a CPU tensor with uniform random values in [0, 1). Panics on error.

func MustRandLike

func MustRandLike(t *Tensor) *Tensor

MustRandLike creates a tensor of uniform random values with the same properties as the input. Panics on error.

func MustRandN

func MustRandN(shape ...int) *Tensor

MustRandN creates a CPU tensor with standard normal random values. Panics on error.

func MustRandNLike

func MustRandNLike(t *Tensor) *Tensor

MustRandNLike creates a tensor of normal random values with the same properties as the input. Panics on error.

func MustRandNOn

func MustRandNOn(backend Backend, shape ...int) *Tensor

MustRandNOn creates a tensor with normal random values using the given backend. Panics on error.

func MustRandOn

func MustRandOn(backend Backend, shape ...int) *Tensor

MustRandOn creates a tensor with uniform random values using the given backend. Panics on error.

func MustZeros

func MustZeros(shape ...int) *Tensor

MustZeros creates a CPU tensor filled with zeros. Panics on error.

func MustZerosLike

func MustZerosLike(t *Tensor) *Tensor

MustZerosLike creates a tensor of zeros with the same shape and dtype as the input. Panics on error.

func MustZerosOn

func MustZerosOn(backend Backend, shape ...int) *Tensor

MustZerosOn creates a tensor filled with zeros using the given backend. Panics on error.

func NewTensor

func NewTensor(storage Storage, shape []int, stride []int) *Tensor

NewTensor creates a tensor with the given storage, shape, and stride. If stride is nil, a contiguous stride is computed from shape.

func NewTensorFromContext

func NewTensorFromContext(ctx context.Context, shape []int, dtype DType) *Tensor

NewTensorFromContext creates a tensor using storage from the context's pool.

func NewTensorFromPool

func NewTensorFromPool(pool *Pool, shape []int, dtype DType) *Tensor

NewTensorFromPool creates a tensor using storage from the specified pool.

func Ones

func Ones(shape ...int) (*Tensor, error)

Ones creates a CPU tensor filled with ones.

func OnesLike

func OnesLike(t *Tensor) (*Tensor, error)

OnesLike creates a tensor of ones with the same shape and dtype as the input. The tensor is created on CPU.

func OnesLikeOn

func OnesLikeOn(backend Backend, t *Tensor) (*Tensor, error)

OnesLikeOn creates a tensor of ones with the same shape as the input using the given backend.

func OnesOn

func OnesOn(backend Backend, shape ...int) (*Tensor, error)

OnesOn creates a tensor filled with ones using the given backend.

func Rand

func Rand(shape ...int) (*Tensor, error)

Rand creates a CPU tensor with uniform random values in [0, 1).

func RandLike

func RandLike(t *Tensor) (*Tensor, error)

RandLike creates a tensor of uniform random values with the same shape as the input. The tensor is created on CPU.

func RandLikeOn

func RandLikeOn(backend Backend, t *Tensor) (*Tensor, error)

RandLikeOn creates a tensor of uniform random values with the same shape as the input using the given backend.

func RandN

func RandN(shape ...int) (*Tensor, error)

RandN creates a CPU tensor with standard normal random values (mean=0, std=1).

func RandNLike

func RandNLike(t *Tensor) (*Tensor, error)

RandNLike creates a tensor of normal random values with the same shape as the input. The tensor is created on CPU.

func RandNLikeOn

func RandNLikeOn(backend Backend, t *Tensor) (*Tensor, error)

RandNLikeOn creates a tensor of normal random values with the same shape as the input using the given backend.

func RandNOn

func RandNOn(backend Backend, shape ...int) (*Tensor, error)

RandNOn creates a tensor with normal random values using the given backend.

func RandOn

func RandOn(backend Backend, shape ...int) (*Tensor, error)

RandOn creates a tensor with uniform random values using the given backend.

func Zeros

func Zeros(shape ...int) (*Tensor, error)

Zeros creates a CPU tensor filled with zeros.

func ZerosLike

func ZerosLike(t *Tensor) (*Tensor, error)

ZerosLike creates a tensor of zeros with the same shape and dtype as the input. The tensor is created on CPU.

func ZerosLikeOn

func ZerosLikeOn(backend Backend, t *Tensor) (*Tensor, error)

ZerosLikeOn creates a tensor of zeros with the same shape as the input using the given backend.

func ZerosOn

func ZerosOn(backend Backend, shape ...int) (*Tensor, error)

ZerosOn creates a tensor filled with zeros using the given backend.

func (*Tensor) Clone

func (t *Tensor) Clone() *Tensor

Clone creates a deep copy of the tensor. For non-contiguous or offset tensors, clones only the logical data and produces a contiguous output with offset 0. Implements pipz.Cloner[*Tensor].

func (*Tensor) Contiguous

func (t *Tensor) Contiguous() *Tensor

Contiguous returns a contiguous copy of the tensor if it's not already contiguous. If the tensor is already contiguous and has no offset, returns the same tensor.

func (*Tensor) DType

func (t *Tensor) DType() DType

DType returns the data type of the tensor elements.

func (*Tensor) Data

func (t *Tensor) Data() ([]float32, error)

Data returns the tensor data as a float32 slice. For CPU tensors, returns the underlying slice directly. For GPU tensors, copies data to host (allocates new slice).

func (*Tensor) Device

func (t *Tensor) Device() Device

Device returns the device where this tensor resides.

func (*Tensor) Dim

func (t *Tensor) Dim() int

Dim returns the number of dimensions.

func (*Tensor) Free

func (t *Tensor) Free()

Free releases the underlying storage. If the tensor was allocated from a pool, storage is returned to that pool.

func (*Tensor) Int64Data

func (t *Tensor) Int64Data() ([]int64, error)

Int64Data retrieves int64 tensor data. For CPU tensors, returns the underlying slice directly. For GPU tensors, copies data to host (allocates new slice).

func (*Tensor) IsContiguous

func (t *Tensor) IsContiguous() bool

IsContiguous returns true if the tensor memory is contiguous.

func (*Tensor) MustData

func (t *Tensor) MustData() []float32

MustData returns the tensor data, panicking on error. Intended for use in tests.

func (*Tensor) MustInt64Data

func (t *Tensor) MustInt64Data() []int64

MustInt64Data returns the int64 tensor data, panicking on error. Intended for use in tests.

func (*Tensor) Numel

func (t *Tensor) Numel() int

Numel returns the total number of elements in the tensor.

func (*Tensor) Offset

func (t *Tensor) Offset() int

Offset returns the offset into storage.

func (*Tensor) Pool

func (t *Tensor) Pool() *Pool

Pool returns the memory pool associated with this tensor, or nil if none.

func (*Tensor) Reshape

func (t *Tensor) Reshape(shape ...int) (*Tensor, error)

Reshape returns a reshaped tensor. If the tensor is contiguous, returns a view. Otherwise, copies data.

func (*Tensor) SetPool

func (t *Tensor) SetPool(p *Pool)

SetPool sets the memory pool for this tensor. The pool will be used when Free() is called.

func (*Tensor) Shape

func (t *Tensor) Shape() []int

Shape returns the dimensions of the tensor.

func (*Tensor) Size

func (t *Tensor) Size(dim int) int

Size returns the size of the given dimension.

func (*Tensor) Storage

func (t *Tensor) Storage() Storage

Storage returns the underlying storage.

func (*Tensor) Stride

func (t *Tensor) Stride() []int

Stride returns the strides of the tensor.

func (*Tensor) String

func (t *Tensor) String() string

String returns a string representation of the tensor metadata.

func (*Tensor) View

func (t *Tensor) View(shape ...int) (*Tensor, error)

View returns a new tensor with a different shape but sharing the same storage. The tensor must be contiguous.

type TensorFactory

type TensorFactory interface {
	// Zeros creates a tensor filled with zeros.
	Zeros(shape ...int) (*Tensor, error)

	// Ones creates a tensor filled with ones.
	Ones(shape ...int) (*Tensor, error)

	// Empty creates an uninitialized tensor.
	Empty(shape ...int) (*Tensor, error)

	// Full creates a tensor filled with a scalar value.
	Full(value float32, shape ...int) (*Tensor, error)

	// FromSlice creates a tensor from a float32 slice.
	FromSlice(data []float32, shape ...int) (*Tensor, error)

	// Rand creates a tensor with uniform random values in [0, 1).
	Rand(shape ...int) (*Tensor, error)

	// RandN creates a tensor with standard normal random values.
	RandN(shape ...int) (*Tensor, error)

	// Eye creates a 2D identity matrix.
	Eye(n int) (*Tensor, error)

	// Arange creates a 1D tensor with values from start to end with step.
	Arange(start, end, step float32) (*Tensor, error)

	// Linspace creates a 1D tensor with n evenly spaced values.
	Linspace(start, end float32, n int) (*Tensor, error)
}

TensorFactory provides convenience methods for creating tensors.

type Transfer

type Transfer struct {
	// contains filtered or unexported fields
}

Transfer is a chainable operator that moves a tensor to a different backend. This is used for explicit device-to-device transfers in pipelines.

func NewTransfer

func NewTransfer(target Backend) *Transfer

NewTransfer creates a Transfer operator that moves tensors to the target backend.

func (*Transfer) Close

func (t *Transfer) Close() error

Close releases any resources held by this operator.

func (*Transfer) Identity

func (t *Transfer) Identity() pipz.Identity

Identity returns the operator identity.

func (*Transfer) Process

func (t *Transfer) Process(ctx context.Context, tensor *Tensor) (*Tensor, error)

Process transfers the tensor to the target backend.

func (*Transfer) Schema

func (t *Transfer) Schema() pipz.Node

Schema returns the operator schema node.

type Transpose

type Transpose struct {
	// contains filtered or unexported fields
}

Transpose is a chainable operator that transposes two dimensions. This is a backend-agnostic shape operation (creates a view).

func NewTranspose

func NewTranspose(dim0, dim1 int) *Transpose

NewTranspose creates a Transpose operator.

func (*Transpose) Close

func (tr *Transpose) Close() error

Close releases any resources held by this operator.

func (*Transpose) Identity

func (tr *Transpose) Identity() pipz.Identity

Identity returns the operator identity.

func (*Transpose) Process

func (tr *Transpose) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process transposes the specified dimensions.

func (*Transpose) Schema

func (tr *Transpose) Schema() pipz.Node

Schema returns the operator schema node.

type Tril

type Tril struct {
	// contains filtered or unexported fields
}

Tril is a chainable operator that returns the lower triangular part of a 2D tensor. Elements above diagonal + k are zeroed. k=0: main diagonal, k<0: below, k>0: above.

func NewTril

func NewTril(backend CompareOps, k int) *Tril

NewTril creates a Tril operator.

func (*Tril) Close

func (tr *Tril) Close() error

Close releases any resources held by this operator.

func (*Tril) Identity

func (tr *Tril) Identity() pipz.Identity

Identity returns the operator identity.

func (*Tril) Process

func (tr *Tril) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process returns the lower triangular part of the input.

func (*Tril) Schema

func (tr *Tril) Schema() pipz.Node

Schema returns the operator schema.

type UnaryOps

type UnaryOps interface {
	// Activation functions
	ReLU(ctx context.Context, t *Tensor) (*Tensor, error)
	Sigmoid(ctx context.Context, t *Tensor) (*Tensor, error)
	Tanh(ctx context.Context, t *Tensor) (*Tensor, error)
	GELU(ctx context.Context, t *Tensor) (*Tensor, error)
	SiLU(ctx context.Context, t *Tensor) (*Tensor, error)

	// Math functions
	Neg(ctx context.Context, t *Tensor) (*Tensor, error)
	Abs(ctx context.Context, t *Tensor) (*Tensor, error)
	Exp(ctx context.Context, t *Tensor) (*Tensor, error)
	Log(ctx context.Context, t *Tensor) (*Tensor, error)
	Sqrt(ctx context.Context, t *Tensor) (*Tensor, error)
	Square(ctx context.Context, t *Tensor) (*Tensor, error)
	Sign(ctx context.Context, t *Tensor) (*Tensor, error)

	// Trigonometric functions
	Sin(ctx context.Context, t *Tensor) (*Tensor, error)
	Cos(ctx context.Context, t *Tensor) (*Tensor, error)
}

UnaryOps defines element-wise unary operations.

type Unsqueeze

type Unsqueeze struct {
	// contains filtered or unexported fields
}

Unsqueeze is a chainable operator that inserts a dimension of size 1. Backend-agnostic.

func NewUnsqueeze

func NewUnsqueeze(dim int) *Unsqueeze

NewUnsqueeze creates an Unsqueeze operator.

func (*Unsqueeze) Close

func (u *Unsqueeze) Close() error

Close releases any resources held by this operator.

func (*Unsqueeze) Identity

func (u *Unsqueeze) Identity() pipz.Identity

Identity returns the operator identity.

func (*Unsqueeze) Process

func (u *Unsqueeze) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process unsqueezes the tensor.

func (*Unsqueeze) Schema

func (u *Unsqueeze) Schema() pipz.Node

Schema returns the operator schema.

type Var

type Var struct {
	// contains filtered or unexported fields
}

Var is a chainable operator that computes variance along dimensions. Uses Bessel's correction (N-1) by default for unbiased estimation.

func NewVar

func NewVar(backend ReduceOps, keepdim bool, correction int, dims ...int) *Var

NewVar creates a Var operator.

func (*Var) Close

func (v *Var) Close() error

Close releases any resources held by this operator.

func (*Var) Identity

func (v *Var) Identity() pipz.Identity

Identity returns the operator identity.

func (*Var) Process

func (v *Var) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process computes variance along the specified dimensions.

func (*Var) Schema

func (v *Var) Schema() pipz.Node

Schema returns the operator schema.

type View

type View struct {
	// contains filtered or unexported fields
}

View is a chainable operator that creates a view with the given shape. The tensor must be contiguous. Use Reshape for non-contiguous tensors. Backend-agnostic.

func NewView

func NewView(shape ...int) *View

NewView creates a View operator.

func (*View) Close

func (v *View) Close() error

Close releases any resources held by this operator.

func (*View) Identity

func (v *View) Identity() pipz.Identity

Identity returns the operator identity.

func (*View) Process

func (v *View) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process creates a view of the tensor.

func (*View) Schema

func (v *View) Schema() pipz.Node

Schema returns the operator schema.

type Where

type Where struct {
	// contains filtered or unexported fields
}

Where is a chainable operator that selects elements based on a condition. For each element: if condition > 0, select from input; otherwise select from other.

func NewWhere

func NewWhere(backend CompareOps, condition, other *Tensor) *Where

NewWhere creates a Where operator.

func (*Where) Close

func (w *Where) Close() error

Close releases any resources held by this operator.

func (*Where) Identity

func (w *Where) Identity() pipz.Identity

Identity returns the operator identity.

func (*Where) Process

func (w *Where) Process(ctx context.Context, t *Tensor) (*Tensor, error)

Process selects elements based on the condition.

func (*Where) Schema

func (w *Where) Schema() pipz.Node

Schema returns the operator schema.

type Zeroer

type Zeroer interface {
	Zero() error
}

Zeroer is an interface for storage types that support zeroing.

Directories

Path Synopsis
Package cpu provides CPU backend implementation for tendo.
Package cpu provides CPU backend implementation for tendo.
cuda module
internal
safetensors
Package safetensors implements parsing of the SafeTensors file format.
Package safetensors implements parsing of the SafeTensors file format.
Package models provides model loading and concrete neural network architectures.
Package models provides model loading and concrete neural network architectures.
gpt2
Package gpt2 provides GPT-2 model implementation for inference.
Package gpt2 provides GPT-2 model implementation for inference.
llama
Package llama provides Llama model implementation for inference.
Package llama provides Llama model implementation for inference.
Package nn provides neural network building blocks built on tendo tensors.
Package nn provides neural network building blocks built on tendo tensors.
pkg
cpu module
cuda module
Package testing provides test utilities and helpers for tendo tensor operations.
Package testing provides test utilities and helpers for tendo tensor operations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL