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:
- Elementwise: Add, Sub, Mul, Div, Abs, Exp, Log, Sqrt, Pow
- Matrix: MatMul, Transpose
- Shape: Reshape, Squeeze, Unsqueeze, Slice, Expand, Permute
- Reductions: Sum, Mean, Max, Min, ArgMax, ArgMin
- Activations: ReLU, Sigmoid, Tanh, GELU, SiLU, Softmax, Dropout
- Normalization: LayerNorm, RMSNorm, BatchNorm2d
- Convolution: Conv2d
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
- Variables
- func BFloat() pipz.Chainable[*Tensor]
- func BFloat16SliceToFloat32(bf16 []uint16) []float32
- func BFloat16ToFloat32(b uint16) float32
- func BinaryOp(identity pipz.Identity, other *Tensor, ...) pipz.Chainable[*Tensor]
- func BroadcastShapes(a, b []int) ([]int, error)
- func CanBroadcast(a, b []int) bool
- func ComputeStrides(shape []int) []int
- func Conv2dOutputShape(inputShape, weightShape []int, config Conv2dConfig) []int
- func CoordsToFlat(coords, strides []int) int
- func Fallback(name string, processors ...pipz.Chainable[*Tensor]) *pipz.Fallback[*Tensor]
- func Filter(name string, predicate func(context.Context, *Tensor) bool, ...) *pipz.Filter[*Tensor]
- func FlatToCoords(flat int, shape []int) []int
- func FlatToReducedIndex(flat int, shape, strides, outStrides []int, skipDims map[int]bool) int
- func FlatToStrided(flat int, shape, stride []int, offset int) int
- func Float() pipz.Chainable[*Tensor]
- func Float16SliceToFloat32(f16 []uint16) []float32
- func Float16ToFloat32(h uint16) float32
- func Float32SliceToBFloat16(f32 []float32) []uint16
- func Float32SliceToFloat16(f32 []float32) []uint16
- func Float32ToBFloat16(f float32) uint16
- func Float32ToFloat16(f float32) uint16
- func Half() pipz.Chainable[*Tensor]
- func InferShape(numel int, shape []int) ([]int, error)
- func IsContiguous(shape, stride []int) bool
- func IsTraining(ctx context.Context) bool
- func IterateSlices(shape, strides []int, dim int, fn func(baseIdx, dimStride int))
- func IterateStrided(numel int, shape, stride []int, offset int, fn func(flatIdx, stridedIdx int))
- func MakeContiguous() pipz.Chainable[*Tensor]
- func Numel(shape []int) int
- func Op(identity pipz.Identity, fn func(context.Context, *Tensor) (*Tensor, error)) pipz.Chainable[*Tensor]
- func Pin() pipz.Chainable[*Tensor]
- func ReduceShapeAndStrides(shape []int, skipDims map[int]bool) (outShape, outStrides []int)
- func RegisterBackend(b StorageBackend)
- func RegisterMemoryAllocator(dt DeviceType, alloc MemoryAllocator)
- func Sequence(name string, processors ...pipz.Chainable[*Tensor]) *pipz.Sequence[*Tensor]
- func SetDefaultDType(d DType)
- func SetDefaultPool(p *Pool)
- func SliceBaseIndex(sliceIdx int, shape, strides []int, dim int) int
- func SqueezeShape(shape []int, dim int) ([]int, error)
- func Switch(name string, condition func(context.Context, *Tensor) string) *pipz.Switch[*Tensor]
- func Sync() pipz.Chainable[*Tensor]
- func Timeout(name string, processor pipz.Chainable[*Tensor], duration time.Duration) *pipz.Timeout[*Tensor]
- func To(device Device) pipz.Chainable[*Tensor]
- func ToBFloat16() pipz.Chainable[*Tensor]
- func ToCPU() pipz.Chainable[*Tensor]
- func ToDType(dtype DType) pipz.Chainable[*Tensor]
- func ToFloat16() pipz.Chainable[*Tensor]
- func ToFloat32() pipz.Chainable[*Tensor]
- func ToGPU(index int) pipz.Chainable[*Tensor]
- func TraceID(ctx context.Context) string
- func Transform(identity pipz.Identity, fn func(context.Context, *Tensor) *Tensor) pipz.Chainable[*Tensor]
- func TransposeShape(shape []int, dim0, dim1 int) ([]int, error)
- func TransposeStride(stride []int, dim0, dim1 int) ([]int, error)
- func UnaryOp(identity pipz.Identity, fn func(*CPUStorage) *CPUStorage) pipz.Chainable[*Tensor]
- func UnaryOpGeneric(identity pipz.Identity, fn func(CPUDataAccessor) *CPUStorage) pipz.Chainable[*Tensor]
- func UnaryOpInPlace(identity pipz.Identity, fn func(*CPUStorage)) pipz.Chainable[*Tensor]
- func Unpin() pipz.Chainable[*Tensor]
- func UnsqueezeShape(shape []int, dim int) ([]int, error)
- func ValidateElementwise(a, b []int) ([]int, error)
- func ValidateMatMul(a, b []int) ([]int, error)
- func WithInference(ctx context.Context) context.Context
- func WithPool(ctx context.Context, p *Pool) context.Context
- func WithTraceID(ctx context.Context, id string) context.Context
- func WithTraining(ctx context.Context) context.Context
- type Abs
- type ActivationOps
- type AdaptiveAvgPool2d
- type AdaptiveMaxPool2d
- type Add
- type ArgMax
- type ArgMin
- type ArithmeticBackend
- type AvgPool2d
- type Backend
- type BackendError
- type BatchNorm2d
- type BinaryOps
- type CPUDataAccessor
- type CPUInt64DataAccessor
- type CPUStorage
- func (s *CPUStorage) Clone() Storage
- func (s *CPUStorage) DType() DType
- func (s *CPUStorage) Data() []float32
- func (s *CPUStorage) Device() Device
- func (s *CPUStorage) Fill(value float32)
- func (s *CPUStorage) Free()
- func (s *CPUStorage) GetData(index int) float32
- func (s *CPUStorage) Int64Data() []int64
- func (s *CPUStorage) Len() int
- func (s *CPUStorage) PoolKey() (numel int, dtype DType, deviceIndex int)
- func (s *CPUStorage) Ptr() uintptr
- func (s *CPUStorage) SetData(index int, value float32)
- func (s *CPUStorage) Size() int
- type CUDAError
- type Cat
- type Clamp
- type CompareBackend
- type CompareOps
- type ContiguousMaker
- type Conv2d
- type Conv2dConfig
- type ConvOps
- type ConvTranspose2d
- type ConvTranspose2dConfig
- type CoreBackend
- type Cos
- type CrossEntropyLoss
- type DType
- type Device
- type DeviceError
- type DeviceInfo
- type DeviceMismatchError
- type DeviceType
- type Div
- type Dropout
- type Embedding
- type EmbeddingOps
- type ErrNotImplemented
- type Exp
- type Expand
- type Filler
- type Flatten
- type GELU
- type GroupNorm
- type HostCopier
- type InstanceNorm2d
- type Int64HostCopier
- type L1Loss
- type LayerNorm
- type LeakyReLU
- type Log
- type LogSoftmax
- type LossOps
- type MSELoss
- type MatMul
- type MatrixBackend
- type MatrixOps
- type Max
- type MaxPool2d
- type Mean
- type MemoryAllocator
- type Min
- type MockStorage
- type Mul
- type NLLLoss
- type Narrow
- type Neg
- type NeuralBackend
- type NormOps
- type Permute
- type Pool
- func (p *Pool) AllocCPU(numel int, dtype DType) *CPUStorage
- func (p *Pool) AllocCUDA(numel int, dtype DType, device int) (uintptr, error)
- func (p *Pool) Clear()
- func (p *Pool) FreeCPU(storage *CPUStorage)
- func (p *Pool) FreeCUDA(ptr uintptr, numel int, dtype DType, device int)
- func (p *Pool) MaxCacheSize() int64
- func (p *Pool) SetContext(ctx context.Context)
- func (p *Pool) SetMaxCacheSize(bytes int64)
- func (p *Pool) Stats() PoolStats
- type Pool2dConfig
- type PoolAllocator
- type PoolOps
- type PoolStats
- type PoolableStorage
- type Pow
- type Prod
- type RMSNorm
- type ReLU
- type ReduceBackend
- type ReduceOps
- type Reshape
- type SafeTensorInfo
- type SafeTensorsFile
- func (f *SafeTensorsFile) Close() error
- func (f *SafeTensorsFile) Load(name string, backend StorageBackend, targetDType *DType) (*Tensor, error)
- func (f *SafeTensorsFile) LoadCPU(name string, targetDType *DType) (*Tensor, error)
- func (f *SafeTensorsFile) NumTensors() int
- func (f *SafeTensorsFile) Path() string
- func (f *SafeTensorsFile) TensorInfo(name string) *SafeTensorInfo
- func (f *SafeTensorsFile) Tensors() map[string]SafeTensorInfo
- type ShapeError
- type ShapeOps
- type SiLU
- type Sigmoid
- type Sign
- type Sin
- type Slice
- type Softmax
- type Sqrt
- type Square
- type Squeeze
- type Stack
- type Std
- type Storage
- type StorageBackend
- type StorageOps
- type Sub
- type Sum
- type Syncer
- type T
- type Tanh
- type Tensor
- func Arange(start, end, step float32) (*Tensor, error)
- func ArangeOn(backend Backend, start, end, step float32) (*Tensor, error)
- func Empty(shape ...int) (*Tensor, error)
- func EmptyLike(t *Tensor) (*Tensor, error)
- func EmptyLikeOn(backend Backend, t *Tensor) (*Tensor, error)
- func EmptyOn(backend Backend, shape ...int) (*Tensor, error)
- func Eye(n int) (*Tensor, error)
- func EyeOn(backend Backend, n int) (*Tensor, error)
- func FromSlice(data []float32, shape ...int) (*Tensor, error)
- func FromSliceOn(backend Backend, data []float32, shape ...int) (*Tensor, error)
- func Full(value float32, shape ...int) (*Tensor, error)
- func FullOn(backend Backend, value float32, shape ...int) (*Tensor, error)
- func Linspace(start, end float32, n int) (*Tensor, error)
- func LinspaceOn(backend Backend, start, end float32, n int) (*Tensor, error)
- func MustArange(start, end, step float32) *Tensor
- func MustArangeOn(backend Backend, start, end, step float32) *Tensor
- func MustEmpty(shape ...int) *Tensor
- func MustEmptyLike(t *Tensor) *Tensor
- func MustEmptyOn(backend Backend, shape ...int) *Tensor
- func MustEye(n int) *Tensor
- func MustEyeOn(backend Backend, n int) *Tensor
- func MustFromSlice(data []float32, shape ...int) *Tensor
- func MustFromSliceOn(backend Backend, data []float32, shape ...int) *Tensor
- func MustFull(value float32, shape ...int) *Tensor
- func MustFullOn(backend Backend, value float32, shape ...int) *Tensor
- func MustLinspace(start, end float32, n int) *Tensor
- func MustLinspaceOn(backend Backend, start, end float32, n int) *Tensor
- func MustOnes(shape ...int) *Tensor
- func MustOnesLike(t *Tensor) *Tensor
- func MustOnesOn(backend Backend, shape ...int) *Tensor
- func MustRand(shape ...int) *Tensor
- func MustRandLike(t *Tensor) *Tensor
- func MustRandN(shape ...int) *Tensor
- func MustRandNLike(t *Tensor) *Tensor
- func MustRandNOn(backend Backend, shape ...int) *Tensor
- func MustRandOn(backend Backend, shape ...int) *Tensor
- func MustZeros(shape ...int) *Tensor
- func MustZerosLike(t *Tensor) *Tensor
- func MustZerosOn(backend Backend, shape ...int) *Tensor
- func NewTensor(storage Storage, shape []int, stride []int) *Tensor
- func NewTensorFromContext(ctx context.Context, shape []int, dtype DType) *Tensor
- func NewTensorFromPool(pool *Pool, shape []int, dtype DType) *Tensor
- func Ones(shape ...int) (*Tensor, error)
- func OnesLike(t *Tensor) (*Tensor, error)
- func OnesLikeOn(backend Backend, t *Tensor) (*Tensor, error)
- func OnesOn(backend Backend, shape ...int) (*Tensor, error)
- func Rand(shape ...int) (*Tensor, error)
- func RandLike(t *Tensor) (*Tensor, error)
- func RandLikeOn(backend Backend, t *Tensor) (*Tensor, error)
- func RandN(shape ...int) (*Tensor, error)
- func RandNLike(t *Tensor) (*Tensor, error)
- func RandNLikeOn(backend Backend, t *Tensor) (*Tensor, error)
- func RandNOn(backend Backend, shape ...int) (*Tensor, error)
- func RandOn(backend Backend, shape ...int) (*Tensor, error)
- func Zeros(shape ...int) (*Tensor, error)
- func ZerosLike(t *Tensor) (*Tensor, error)
- func ZerosLikeOn(backend Backend, t *Tensor) (*Tensor, error)
- func ZerosOn(backend Backend, shape ...int) (*Tensor, error)
- func (t *Tensor) Clone() *Tensor
- func (t *Tensor) Contiguous() *Tensor
- func (t *Tensor) DType() DType
- func (t *Tensor) Data() ([]float32, error)
- func (t *Tensor) Device() Device
- func (t *Tensor) Dim() int
- func (t *Tensor) Free()
- func (t *Tensor) Int64Data() ([]int64, error)
- func (t *Tensor) IsContiguous() bool
- func (t *Tensor) MustData() []float32
- func (t *Tensor) MustInt64Data() []int64
- func (t *Tensor) Numel() int
- func (t *Tensor) Offset() int
- func (t *Tensor) Pool() *Pool
- func (t *Tensor) Reshape(shape ...int) (*Tensor, error)
- func (t *Tensor) SetPool(p *Pool)
- func (t *Tensor) Shape() []int
- func (t *Tensor) Size(dim int) int
- func (t *Tensor) Storage() Storage
- func (t *Tensor) Stride() []int
- func (t *Tensor) String() string
- func (t *Tensor) View(shape ...int) (*Tensor, error)
- type TensorFactory
- type Transfer
- type Transpose
- type Tril
- type UnaryOps
- type Unsqueeze
- type Var
- type View
- type Where
- type Zeroer
Constants ¶
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.
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.
const VariantDType capitan.Variant = "tendo.DType"
VariantDType is the capitan variant for tensor data types.
Variables ¶
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.
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.
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.
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.
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.
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.
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.
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.
var ( IdentityMatMul = pipz.NewIdentity("matmul", "Matrix multiplication") IdentityTranspose = pipz.NewIdentity("transpose", "Matrix transpose") IdentityT = pipz.NewIdentity("t", "Transpose last two dimensions") )
Matrix operation identities.
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.
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.
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.
var ( IdentityConv2d = pipz.NewIdentity("conv2d", "2D convolution") IdentityConvTranspose2d = pipz.NewIdentity("convtranspose2d", "2D transposed convolution") )
Convolution operation identities.
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.
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.
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.
var ( KeySourceDevice = capitan.NewKey[Device]("source_device", VariantDevice) KeyTargetDevice = capitan.NewKey[Device]("target_device", VariantDevice) )
Signal field keys for transfer.
var (
IdentityEmbedding = pipz.NewIdentity("embedding", "Embedding lookup")
)
Embedding operation identities.
var (
IdentityTransfer = pipz.NewIdentity("transfer", "Device transfer")
)
Transfer operation identities.
var (
OpConv2d = capitan.NewSignal("tendo.op.conv2d", "2D convolution")
)
Observability signals for convolution.
var (
OpTransfer = capitan.NewSignal("tendo.op.transfer", "Device transfer")
)
Observability signals for device transfer.
Functions ¶
func BFloat16SliceToFloat32 ¶
BFloat16SliceToFloat32 converts a slice of bfloat16 to float32.
func BFloat16ToFloat32 ¶
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 ¶
BroadcastShapes computes the broadcast shape of two shapes. Returns an error if the shapes are not broadcastable.
func CanBroadcast ¶
CanBroadcast returns true if two shapes can be broadcast together.
func ComputeStrides ¶
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 ¶
CoordsToFlat converts multi-dimensional coordinates to a flat index using strides.
func Fallback ¶
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 ¶
FlatToCoords converts a flat index to multi-dimensional coordinates.
func FlatToReducedIndex ¶
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 ¶
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 Float16SliceToFloat32 ¶
Float16SliceToFloat32 converts a slice of float16 to float32.
func Float16ToFloat32 ¶
Float16ToFloat32 converts a float16 (IEEE 754 half-precision) to float32.
func Float32SliceToBFloat16 ¶
Float32SliceToBFloat16 converts a slice of float32 to bfloat16.
func Float32SliceToFloat16 ¶
Float32SliceToFloat16 converts a slice of float32 to float16.
func Float32ToBFloat16 ¶
Float32ToBFloat16 converts a float32 to bfloat16. BFloat16 is the upper 16 bits of float32 with rounding.
func Float32ToFloat16 ¶
Float32ToFloat16 converts a float32 to float16 (IEEE 754 half-precision). Returns the 16-bit representation as uint16.
func InferShape ¶
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 ¶
IsContiguous returns true if the given shape and stride represent contiguous memory.
func IsTraining ¶
IsTraining returns true if the context has training mode enabled.
func IterateSlices ¶
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 ¶
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 ¶
MakeContiguous returns a Chainable that makes the tensor contiguous in memory. If already contiguous, returns the tensor unchanged.
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 ¶
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 ¶
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 ¶
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 SliceBaseIndex ¶
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 ¶
SqueezeShape removes a dimension of size 1 from the shape.
func Switch ¶
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 ¶
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 ¶
To returns a Chainable that moves the tensor to the specified device. If already on the target device, returns the tensor unchanged.
func ToBFloat16 ¶
ToBFloat16 returns a Chainable that converts a tensor to BFloat16.
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 ¶
TransposeShape returns the shape after transposing two dimensions.
func TransposeStride ¶
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 ¶
UnaryOpInPlace creates an in-place unary tensor operation. The input tensor is modified and returned.
func UnsqueezeShape ¶
UnsqueezeShape inserts a dimension of size 1 at the given position.
func ValidateElementwise ¶
ValidateElementwise validates shapes for element-wise operations and returns the output shape.
func ValidateMatMul ¶
ValidateMatMul validates shapes for matrix multiplication and returns the output shape. Supports 2D matrices and batched matrix multiplication.
func WithInference ¶
WithInference returns a context with training mode disabled (inference mode). This is the default behavior.
func WithPool ¶
WithPool returns a context with the given memory pool. Operations using this context will allocate from the pool.
func WithTraceID ¶
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.
Types ¶
type Abs ¶
type Abs struct {
// contains filtered or unexported fields
}
Abs is a chainable operator that takes the absolute value of each element.
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) 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) 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.
type ArgMax ¶
type ArgMax struct {
// contains filtered or unexported fields
}
ArgMax is a chainable operator that returns indices of max values along a dimension.
type ArgMin ¶
type ArgMin struct {
// contains filtered or unexported fields
}
ArgMin is a chainable operator that returns indices of min values along a dimension.
type ArithmeticBackend ¶
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.
type Backend ¶
type Backend interface {
CoreBackend
ArithmeticBackend
MatrixBackend
NeuralBackend
ReduceBackend
CompareBackend
}
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) 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) Data ¶
func (s *CPUStorage) Data() []float32
Data returns the underlying float32 slice for direct access. Panics if the storage contains int64 data.
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 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 ¶
NewCat creates a Cat operator. If backend is nil, uses CPU-only fallback implementation.
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.
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 ¶
NewConv2dSimple is a convenience constructor for standard convolution. Applies same padding to keep spatial dimensions.
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) 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.
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) 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.
type Device ¶
type Device struct {
Type DeviceType
Index int
}
Device represents a compute device (CPU or GPU).
func CUDADevice ¶
CUDADevice returns a CUDA device with the given index.
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 ¶
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.
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.
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].
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 ¶
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.
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.
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 ¶
NewFlatten creates a Flatten operator.
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))).
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 ¶
NewGroupNorm creates a GroupNorm operator.
type HostCopier ¶
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) Schema ¶
func (i *InstanceNorm2d) Schema() pipz.Node
Schema returns the operator schema node.
type Int64HostCopier ¶
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".
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.
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.
type Log ¶
type Log struct {
// contains filtered or unexported fields
}
Log is a chainable operator that computes the natural logarithm of each element.
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) 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 ¶
NewMSELoss creates an MSELoss operator.
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.
type MatrixBackend ¶
type MatrixBackend interface {
MatrixOps
}
MatrixBackend provides 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.
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.
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.
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.
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) 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.
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 ¶
NewNLLLoss creates an NLLLoss operator.
type Narrow ¶
type Narrow struct {
// contains filtered or unexported fields
}
Narrow is an alias for Slice. Backend-agnostic.
type Neg ¶
type Neg struct {
// contains filtered or unexported fields
}
Neg is a chainable operator that negates each element.
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.
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 PoolFromContext ¶
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 ¶
AllocCUDA allocates or retrieves CUDA memory of the given size. Returns the device pointer and an error if CUDA is unavailable.
func (*Pool) FreeCPU ¶
func (p *Pool) FreeCPU(storage *CPUStorage)
FreeCPU returns a CPU storage to the pool for reuse.
func (*Pool) MaxCacheSize ¶
MaxCacheSize returns the current maximum cache size (0 = unlimited).
func (*Pool) SetContext ¶
SetContext sets the context used for emitting pool events.
func (*Pool) SetMaxCacheSize ¶
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.
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.
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.
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 ¶
NewRMSNorm creates an RMSNorm operator.
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).
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.
type SafeTensorInfo ¶
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) 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) 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 ¶
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).
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)).
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.
type Sin ¶
type Sin struct {
// contains filtered or unexported fields
}
Sin is a chainable operator that computes the sine of each element.
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.
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.
type Sqrt ¶
type Sqrt struct {
// contains filtered or unexported fields
}
Sqrt is a chainable operator that computes the square root of each element.
type Square ¶
type Square struct {
// contains filtered or unexported fields
}
Square is a chainable operator that squares each element.
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 ¶
NewSqueezeDim creates a Squeeze operator that squeezes a specific dimension.
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 ¶
NewStack creates a Stack operator. If backend is nil, uses CPU-only fallback implementation.
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.
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.
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.
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.
type Tanh ¶
type Tanh struct {
// contains filtered or unexported fields
}
Tanh is a chainable operator that applies the tanh activation function.
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 EmptyLike ¶
EmptyLike creates an uninitialized tensor with the same shape and dtype as the input. The tensor is created on CPU.
func EmptyLikeOn ¶
EmptyLikeOn creates an uninitialized tensor with the same shape as the input using the given backend.
func FromSliceOn ¶
FromSliceOn creates a tensor from a float32 slice using the given backend.
func Linspace ¶
Linspace creates a 1D CPU tensor with n evenly spaced values from start to end (inclusive).
func LinspaceOn ¶
LinspaceOn creates a 1D tensor with evenly spaced values using the given backend.
func MustArange ¶
MustArange creates a 1D CPU tensor with values from start to end (exclusive) with step. Panics on error.
func MustArangeOn ¶
MustArangeOn creates a 1D tensor with values from start to end using the given backend. Panics on error.
func MustEmpty ¶
MustEmpty creates an uninitialized CPU tensor with the given shape. Panics on error.
func MustEmptyLike ¶
MustEmptyLike creates an uninitialized tensor with the same shape and dtype as the input. Panics on error.
func MustEmptyOn ¶
MustEmptyOn creates an uninitialized tensor using the given backend. Panics on error.
func MustFromSlice ¶
MustFromSlice creates a CPU tensor from a float32 slice with the given shape. Panics on error.
func MustFromSliceOn ¶
MustFromSliceOn creates a tensor from a float32 slice using the given backend. Panics on error.
func MustFullOn ¶
MustFullOn creates a tensor filled with the given value using the given backend. Panics on error.
func MustLinspace ¶
MustLinspace creates a 1D CPU tensor with n evenly spaced values from start to end. Panics on error.
func MustLinspaceOn ¶
MustLinspaceOn creates a 1D tensor with evenly spaced values using the given backend. Panics on error.
func MustOnesLike ¶
MustOnesLike creates a tensor of ones with the same shape and dtype as the input. Panics on error.
func MustOnesOn ¶
MustOnesOn creates a tensor filled with ones using the given backend. Panics on error.
func MustRand ¶
MustRand creates a CPU tensor with uniform random values in [0, 1). Panics on error.
func MustRandLike ¶
MustRandLike creates a tensor of uniform random values with the same properties as the input. Panics on error.
func MustRandN ¶
MustRandN creates a CPU tensor with standard normal random values. Panics on error.
func MustRandNLike ¶
MustRandNLike creates a tensor of normal random values with the same properties as the input. Panics on error.
func MustRandNOn ¶
MustRandNOn creates a tensor with normal random values using the given backend. Panics on error.
func MustRandOn ¶
MustRandOn creates a tensor with uniform random values using the given backend. Panics on error.
func MustZerosLike ¶
MustZerosLike creates a tensor of zeros with the same shape and dtype as the input. Panics on error.
func MustZerosOn ¶
MustZerosOn creates a tensor filled with zeros using the given backend. Panics on error.
func NewTensor ¶
NewTensor creates a tensor with the given storage, shape, and stride. If stride is nil, a contiguous stride is computed from shape.
func NewTensorFromContext ¶
NewTensorFromContext creates a tensor using storage from the context's pool.
func NewTensorFromPool ¶
NewTensorFromPool creates a tensor using storage from the specified pool.
func OnesLike ¶
OnesLike creates a tensor of ones with the same shape and dtype as the input. The tensor is created on CPU.
func OnesLikeOn ¶
OnesLikeOn creates a tensor of ones with the same shape as the input using the given backend.
func RandLike ¶
RandLike creates a tensor of uniform random values with the same shape as the input. The tensor is created on CPU.
func RandLikeOn ¶
RandLikeOn creates a tensor of uniform random values with the same shape as the input using the given backend.
func RandNLike ¶
RandNLike creates a tensor of normal random values with the same shape as the input. The tensor is created on CPU.
func RandNLikeOn ¶
RandNLikeOn creates a tensor of normal random values with the same shape as the input using the given backend.
func ZerosLike ¶
ZerosLike creates a tensor of zeros with the same shape and dtype as the input. The tensor is created on CPU.
func ZerosLikeOn ¶
ZerosLikeOn creates a tensor of zeros with the same shape as the input using the given backend.
func (*Tensor) Clone ¶
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 ¶
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) Data ¶
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) 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 ¶
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 ¶
IsContiguous returns true if the tensor memory is contiguous.
func (*Tensor) MustData ¶
MustData returns the tensor data, panicking on error. Intended for use in tests.
func (*Tensor) MustInt64Data ¶
MustInt64Data returns the int64 tensor data, panicking on error. Intended for use in tests.
func (*Tensor) Reshape ¶
Reshape returns a reshaped tensor. If the tensor is contiguous, returns a view. Otherwise, copies data.
func (*Tensor) SetPool ¶
SetPool sets the memory pool for this tensor. The pool will be used when Free() is called.
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 ¶
NewTransfer creates a Transfer operator that moves tensors to the target backend.
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 ¶
NewTranspose creates a Transpose operator.
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.
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 ¶
NewUnsqueeze creates an Unsqueeze operator.
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.
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.
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.
Source Files
¶
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. |