core

package
v1.38.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Overview

Package core provides core neural network layer implementations.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Stability: stable

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core neural network layer implementations.

Package core provides core neural network layer implementations.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides the Shape layer for the Zerfoo ML framework.

Package core provides core neural network layer implementations.

Package core provides core layer implementations for the Zerfoo ML framework.

Package core provides core layer implementations for the Zerfoo ML framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAdd added in v0.2.1

func BuildAdd[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	name string,
	params map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildAdd constructs an Add node from attributes.

func BuildCast added in v0.2.0

func BuildCast[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildCast constructs a Cast node.

func BuildConcat added in v0.2.0

func BuildConcat[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildConcat constructs a Concat node, extracting the axis from attributes.

func BuildConstantOfShape added in v0.2.1

func BuildConstantOfShape[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], attrs map[string]any,
) (graph.Node[T], error)

BuildConstantOfShape constructs a ConstantOfShape node from attributes.

func BuildConv2d added in v0.2.1

func BuildConv2d[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildConv2d constructs a Conv2d layer from registry attributes. Expects optional int64 or []int64 attributes: "strides", "pads", "dilations", "group".

func BuildCos added in v0.2.1

func BuildCos[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildCos constructs a Cos node from attributes.

func BuildDiv added in v0.2.1

func BuildDiv[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildDiv constructs a Div node from attributes.

func BuildEqual added in v0.2.1

func BuildEqual[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildEqual constructs an Equal node from attributes.

func BuildExpand added in v0.2.1

func BuildExpand[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildExpand constructs an Expand node from attributes.

func BuildFFN added in v0.2.1

func BuildFFN[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	name string,
	params map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildFFN creates an FFN layer from ZMF parameters and attributes.

func BuildFiLM added in v0.2.1

func BuildFiLM[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	name string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildFiLM constructs a FiLM node from attributes. Required attributes: - "context_dim" (int): dimension of the context input - "feature_dim" (int): dimension of the feature input

func BuildGemm added in v0.2.1

func BuildGemm[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], attrs map[string]any,
) (graph.Node[T], error)

BuildGemm constructs a Gemm node from attributes.

func BuildGlobalAveragePool added in v0.2.1

func BuildGlobalAveragePool[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildGlobalAveragePool constructs a GlobalAveragePool node for the layer registry.

func BuildGreater added in v0.2.1

func BuildGreater[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildGreater constructs a Greater node from attributes.

func BuildLessOrEqual added in v0.2.1

func BuildLessOrEqual[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildLessOrEqual constructs a LessOrEqual node from attributes.

func BuildMatMul added in v0.2.0

func BuildMatMul[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildMatMul constructs a new MatMul node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildMax added in v0.2.1

func BuildMax[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildMax constructs a Max node from attributes.

func BuildMixtureOfExperts added in v0.2.1

func BuildMixtureOfExperts[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildMixtureOfExperts constructs a MixtureOfExperts layer from ZMF attributes. Required attributes: "num_experts" (int or int64), "top_k" (int or int64). Note: Expert sub-graphs are not populated (tech debt: ZMF sub-graph support not yet implemented). Experts must be injected manually for real use.

func BuildMoEGate added in v0.2.1

func BuildMoEGate[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildMoEGate constructs a MoEGate layer from ZMF attributes. Required attribute: "top_k" (int or int64).

func BuildMod added in v0.2.1

func BuildMod[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildMod constructs a Mod node from attributes.

func BuildMul added in v0.2.0

func BuildMul[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildMul constructs a new Mul node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildNeg added in v0.2.1

func BuildNeg[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildNeg constructs a Neg node from attributes.

func BuildOr added in v0.2.1

func BuildOr[T tensor.Numeric](
	engine compute.Engine[T], ops numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildOr constructs an Or node from attributes.

func BuildPad added in v0.2.1

func BuildPad[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildPad constructs a Pad layer from ZMF attributes. Supported attribute keys: "pads" ([]int64), "constant_value" (float32/float64).

func BuildPow added in v0.2.1

func BuildPow[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildPow constructs a Pow node from attributes.

func BuildRange added in v0.2.1

func BuildRange[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildRange constructs a Range node from attributes.

func BuildReduceMean added in v0.2.1

func BuildReduceMean[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], attrs map[string]any,
) (graph.Node[T], error)

BuildReduceMean constructs a ReduceMean node from attributes.

func BuildReshape added in v0.2.0

func BuildReshape[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildReshape constructs a Reshape node, parsing target shape from attributes.

func BuildResize added in v0.2.1

func BuildResize[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildResize constructs a Resize layer from registry attributes. Reads "mode" (string), "scales" ([]float64), and/or "sizes" ([]int64). Returns an error if neither scales nor sizes are provided.

func BuildRotaryEmbedding added in v0.2.0

func BuildRotaryEmbedding[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	name string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildRotaryEmbedding constructs a new RotaryPositionalEmbedding node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildScatterND added in v0.2.1

func BuildScatterND[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildScatterND constructs a ScatterND node from attributes.

func BuildShape added in v0.2.0

func BuildShape[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildShape constructs a new Shape node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildSin added in v0.2.1

func BuildSin[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildSin constructs a Sin node from attributes.

func BuildSlice added in v0.2.1

func BuildSlice[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildSlice constructs a Slice layer from ZMF attributes. Supported attribute keys: "starts", "ends", "axes", "steps" (all []int64).

func BuildSpectralFingerprint added in v0.2.1

func BuildSpectralFingerprint[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildSpectralFingerprint constructs a SpectralFingerprint node from attributes. Required attributes: - "window" (int): window length to analyze (must be > 1) - "top_k" (int): number of non-DC bins to output (must be > 0).

func BuildSqrt added in v0.2.1

func BuildSqrt[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildSqrt constructs a Sqrt node from attributes.

func BuildSqueeze added in v0.2.1

func BuildSqueeze[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], attrs map[string]any,
) (graph.Node[T], error)

BuildSqueeze constructs a Squeeze node from attributes.

func BuildSub added in v0.2.0

func BuildSub[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	_ map[string]interface{},
) (graph.Node[T], error)

BuildSub constructs a new Sub node for the given compute engine. It conforms to the layer registry builder signature used by the graph builder.

func BuildTile added in v0.2.1

func BuildTile[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildTile constructs a Tile node from attributes.

func BuildTopK added in v0.2.1

func BuildTopK[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildTopK constructs a TopK layer from ZMF attributes. Required: "k" (int or int64). Optional: "axis" (int/int64, default -1), "largest" (int64, 1=true, 0=false; default 1), "sorted" (int64; default 1).

func BuildTrilu added in v0.2.1

func BuildTrilu[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], attrs map[string]any,
) (graph.Node[T], error)

BuildTrilu constructs a Trilu node from attributes.

func BuildUnsqueeze added in v0.2.0

func BuildUnsqueeze[T tensor.Numeric](
	engine compute.Engine[T],
	_ numeric.Arithmetic[T],
	_ string,
	_ map[string]*graph.Parameter[T],
	attributes map[string]interface{},
) (graph.Node[T], error)

BuildUnsqueeze constructs a new Unsqueeze node for the given compute engine. It parses optional "axes" attributes from the attributes map.

func BuildWhere added in v0.2.1

func BuildWhere[T tensor.Numeric](
	engine compute.Engine[T], _ numeric.Arithmetic[T], _ string,
	_ map[string]*graph.Parameter[T], _ map[string]any,
) (graph.Node[T], error)

BuildWhere constructs a Where node from attributes.

Types

type Add added in v0.2.1

type Add[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Add represents an element-wise addition node.

func NewAdd added in v0.2.1

func NewAdd[T tensor.Numeric](engine compute.Engine[T]) *Add[T]

NewAdd creates a new Add node.

func (*Add[T]) Attributes added in v0.2.1

func (a *Add[T]) Attributes() map[string]any

Attributes returns the attributes of the Add node.

func (*Add[T]) Backward added in v0.2.1

func (a *Add[T]) Backward(_ context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass of the Add node.

func (*Add[T]) Forward added in v0.2.1

func (a *Add[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the Add node.

func (*Add[T]) OpType added in v0.2.1

func (a *Add[T]) OpType() string

OpType returns the operator type of the Add node.

func (*Add[T]) OutputShape added in v0.2.1

func (a *Add[T]) OutputShape() []int

OutputShape returns the output shape of the Add node.

func (*Add[T]) Parameters added in v0.2.1

func (a *Add[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the Add node.

type Bias

type Bias[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Bias adds a bias vector to its input.

func NewBias

func NewBias[T tensor.Numeric](name string, engine compute.Engine[T], ops numeric.Arithmetic[T], size int, opts ...BiasOption[T]) (*Bias[T], error)

NewBias creates a new Bias layer with default tensor and parameter creation.

func NewBiasFromParam added in v0.2.0

func NewBiasFromParam[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], biases *graph.Parameter[T]) *Bias[T]

NewBiasFromParam creates a new Bias layer from an existing biases parameter.

func NewBiasWithFactories

func NewBiasWithFactories[T tensor.Numeric](
	name string, engine compute.Engine[T], ops numeric.Arithmetic[T], size int,
	newTensor func([]int, []T) (*tensor.TensorNumeric[T], error),
	newParameter func(string, *tensor.TensorNumeric[T], func([]int, []T) (*tensor.TensorNumeric[T], error)) (*graph.Parameter[T], error),
	initializer func(size int) []T,
) (*Bias[T], error)

NewBiasWithFactories creates a new Bias layer with custom tensor and parameter creation functions.

func (*Bias[T]) Attributes added in v0.2.0

func (b *Bias[T]) Attributes() map[string]interface{}

Attributes returns nil for the Bias layer.

func (*Bias[T]) Backward

func (b *Bias[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients.

func (*Bias[T]) Forward

func (b *Bias[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass: output = input + biases.

func (*Bias[T]) OpType added in v0.2.0

func (b *Bias[T]) OpType() string

OpType returns the operation type of the Bias layer.

func (*Bias[T]) OutputShape

func (b *Bias[T]) OutputShape() []int

OutputShape returns the output shape of the Bias layer.

func (*Bias[T]) Parameters

func (b *Bias[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the Bias layer.

func (*Bias[T]) SetName

func (b *Bias[T]) SetName(name string)

SetName sets the name of the Bias layer.

type BiasOption added in v0.2.0

type BiasOption[T tensor.Numeric] func(*BiasOptions[T])

BiasOption is a function that applies an option to BiasOptions.

func WithBiasInitializer added in v0.2.0

func WithBiasInitializer[T tensor.Numeric](initializer func(size int) []T) BiasOption[T]

WithBiasInitializer sets a custom initializer for the bias vector.

type BiasOptions added in v0.2.0

type BiasOptions[T tensor.Numeric] struct {
	Initializer func(size int) []T
}

BiasOptions holds configuration options for the Bias layer.

type Cast added in v0.2.0

type Cast[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Cast is a layer that converts tensor elements to a different data type.

func NewCast added in v0.2.0

func NewCast[T tensor.Numeric](engine compute.Engine[T]) *Cast[T]

NewCast creates a new Cast layer.

func (*Cast[T]) Attributes added in v0.2.0

func (c *Cast[T]) Attributes() map[string]interface{}

Attributes returns nil for the Cast layer.

func (*Cast[T]) Backward added in v0.2.0

func (c *Cast[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Cast layer.

func (*Cast[T]) Forward added in v0.2.0

func (c *Cast[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the cast operation (for same type, this is essentially a copy).

func (*Cast[T]) OpType added in v0.2.0

func (c *Cast[T]) OpType() string

OpType returns the operation type of the Cast layer.

func (*Cast[T]) OutputShape added in v0.2.0

func (c *Cast[T]) OutputShape() []int

OutputShape returns the output shape of the Cast layer.

func (*Cast[T]) Parameters added in v0.2.0

func (c *Cast[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Cast layer.

type Concat added in v0.2.0

type Concat[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Concat is a layer that concatenates multiple tensors along a specified axis.

func NewConcat added in v0.2.0

func NewConcat[T tensor.Numeric](engine compute.Engine[T], axis int) *Concat[T]

NewConcat creates a new Concat layer.

func (*Concat[T]) Attributes added in v0.2.0

func (c *Concat[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Concat layer.

func (*Concat[T]) Backward added in v0.2.0

func (c *Concat[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Concat layer.

func (*Concat[T]) Forward added in v0.2.0

func (c *Concat[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the concatenation of input tensors along the specified axis.

func (*Concat[T]) OpType added in v0.2.0

func (c *Concat[T]) OpType() string

OpType returns the operation type of the Concat layer.

func (*Concat[T]) OutputShape added in v0.2.0

func (c *Concat[T]) OutputShape() []int

OutputShape returns the output shape of the Concat layer.

func (*Concat[T]) Parameters added in v0.2.0

func (c *Concat[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Concat layer.

type Constant added in v0.2.1

type Constant[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Constant represents a layer that outputs a constant tensor value. This is used for embedding constant tensors from ONNX TENSOR attributes.

func NewConstant added in v0.2.1

func NewConstant[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	value *tensor.TensorNumeric[T],
) (*Constant[T], error)

NewConstant creates a new Constant layer with the given tensor value.

func NewConstantFromData added in v0.2.1

func NewConstantFromData[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	shape []int,
	data []T,
) (*Constant[T], error)

NewConstantFromData creates a new Constant layer with data and shape.

func (*Constant[T]) Attributes added in v0.2.1

func (c *Constant[T]) Attributes() map[string]interface{}

Attributes returns the node's attributes.

func (*Constant[T]) Backward added in v0.2.1

func (c *Constant[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward performs the backward pass for Constant. Constants produce zero gradients for all inputs since they don't depend on inputs.

func (*Constant[T]) Forward added in v0.2.1

func (c *Constant[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass by returning the constant value. The constant value is independent of inputs.

func (*Constant[T]) GetValue added in v0.2.1

func (c *Constant[T]) GetValue() *tensor.TensorNumeric[T]

GetValue returns the constant tensor value. Useful for inspection and debugging.

func (*Constant[T]) IsMatrix added in v0.2.1

func (c *Constant[T]) IsMatrix() bool

IsMatrix returns true if the constant is a matrix (2-dimensional tensor).

func (*Constant[T]) IsScalar added in v0.2.1

func (c *Constant[T]) IsScalar() bool

IsScalar returns true if the constant is a scalar (0-dimensional tensor).

func (*Constant[T]) IsVector added in v0.2.1

func (c *Constant[T]) IsVector() bool

IsVector returns true if the constant is a vector (1-dimensional tensor).

func (*Constant[T]) Name added in v0.2.1

func (c *Constant[T]) Name() string

Name returns the name of the constant layer.

func (*Constant[T]) NumElements added in v0.2.1

func (c *Constant[T]) NumElements() int

NumElements returns the total number of elements in the constant tensor.

func (*Constant[T]) OpType added in v0.2.1

func (c *Constant[T]) OpType() string

OpType returns the operation type of the node.

func (*Constant[T]) OutputShape added in v0.2.1

func (c *Constant[T]) OutputShape() []int

OutputShape returns the shape of the output tensor.

func (*Constant[T]) Parameters added in v0.2.1

func (c *Constant[T]) Parameters() []*graph.Parameter[T]

Parameters returns the trainable parameters (empty for constant layers).

func (*Constant[T]) SetName added in v0.2.1

func (c *Constant[T]) SetName(name string)

SetName sets the name of the constant layer.

func (*Constant[T]) String added in v0.2.1

func (c *Constant[T]) String() string

String returns a string representation of the constant layer.

type ConstantOfShape added in v0.2.1

type ConstantOfShape[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

ConstantOfShape creates a tensor of a given shape filled with a constant value.

func (*ConstantOfShape[T]) Attributes added in v0.2.1

func (c *ConstantOfShape[T]) Attributes() map[string]any

func (*ConstantOfShape[T]) Backward added in v0.2.1

func (*ConstantOfShape[T]) Forward added in v0.2.1

func (c *ConstantOfShape[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*ConstantOfShape[T]) OpType added in v0.2.1

func (c *ConstantOfShape[T]) OpType() string

func (*ConstantOfShape[T]) OutputShape added in v0.2.1

func (c *ConstantOfShape[T]) OutputShape() []int

func (*ConstantOfShape[T]) Parameters added in v0.2.1

func (c *ConstantOfShape[T]) Parameters() []*graph.Parameter[T]

type Conv1D added in v0.2.1

type Conv1D[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Conv1D implements a 1D convolution layer. Input shape: [batch, channels_in, length] Weight shape: [channels_out, channels_in, kernel_size] Output shape: [batch, channels_out, output_length] where output_length = (length + 2*padding - kernel_size) / stride + 1

func NewConv1D added in v0.2.1

func NewConv1D[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inChannels, outChannels, kernelSize int,
	opts ...Conv1DOption,
) (*Conv1D[T], error)

NewConv1D creates a new Conv1D layer.

func (*Conv1D[T]) Attributes added in v0.2.1

func (c *Conv1D[T]) Attributes() map[string]interface{}

func (*Conv1D[T]) Backward added in v0.2.1

func (c *Conv1D[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients for Conv1D.

func (*Conv1D[T]) Forward added in v0.2.1

func (c *Conv1D[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes 1D convolution. Input: [batch, inChannels, length] Output: [batch, outChannels, outputLength]

func (*Conv1D[T]) OpType added in v0.2.1

func (c *Conv1D[T]) OpType() string

func (*Conv1D[T]) OutputShape added in v0.2.1

func (c *Conv1D[T]) OutputShape() []int

func (*Conv1D[T]) Parameters added in v0.2.1

func (c *Conv1D[T]) Parameters() []*graph.Parameter[T]

type Conv1DOption added in v0.2.1

type Conv1DOption func(c interface{})

Conv1DOption configures Conv1D construction.

func Conv1DPadding added in v0.2.1

func Conv1DPadding(p int) Conv1DOption

Conv1DPadding sets the padding.

func Conv1DStride added in v0.2.1

func Conv1DStride(s int) Conv1DOption

Conv1DStride sets the stride.

func Conv1DWithoutBias added in v0.2.1

func Conv1DWithoutBias() Conv1DOption

Conv1DWithoutBias disables bias.

type Conv2d added in v0.2.1

type Conv2d[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Conv2d performs 2D convolution (inference-only). Forward expects inputs (X, W [,B]) where:

  • X: [N, C_in, H, W]
  • W: [C_out, C_in/groups, kH, kW]
  • B: [C_out] (optional)

func NewConv2d added in v0.2.1

func NewConv2d[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	strides []int,
	pads []int,
	dilations []int,
	groups int,
) *Conv2d[T]

NewConv2d creates a Conv2d layer. strides: [strideH, strideW]; pads: [top, left, bottom, right]; dilations: [dH, dW].

func (*Conv2d[T]) Attributes added in v0.2.1

func (c *Conv2d[T]) Attributes() map[string]interface{}

Attributes returns the layer configuration.

func (*Conv2d[T]) Backward added in v0.2.1

func (c *Conv2d[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward returns nil (inference-only).

func (*Conv2d[T]) Forward added in v0.2.1

func (c *Conv2d[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes 2D convolution via im2col + engine.MatMul.

func (*Conv2d[T]) OpType added in v0.2.1

func (c *Conv2d[T]) OpType() string

OpType returns "Conv".

func (*Conv2d[T]) OutputShape added in v0.2.1

func (c *Conv2d[T]) OutputShape() []int

OutputShape returns the output shape (populated after Forward).

func (*Conv2d[T]) Parameters added in v0.2.1

func (c *Conv2d[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil (no embedded trainable parameters).

type Cos added in v0.2.1

type Cos[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Cos represents an element-wise cosine node.

func (*Cos[T]) Attributes added in v0.2.1

func (c *Cos[T]) Attributes() map[string]any

func (*Cos[T]) Backward added in v0.2.1

func (c *Cos[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Cos[T]) Forward added in v0.2.1

func (c *Cos[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Cos[T]) OpType added in v0.2.1

func (c *Cos[T]) OpType() string

func (*Cos[T]) OutputShape added in v0.2.1

func (c *Cos[T]) OutputShape() []int

func (*Cos[T]) Parameters added in v0.2.1

func (c *Cos[T]) Parameters() []*graph.Parameter[T]

type Dense

type Dense[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Dense is a fully connected layer with optional activation and bias.

func NewDense

func NewDense[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputFeatures, outputFeatures int,
	opts ...DenseOpt[T],
) (*Dense[T], error)

NewDense creates a new Dense layer.

func NewDenseFromParams added in v0.2.0

func NewDenseFromParams[T tensor.Numeric](linear *Linear[T], bias *Bias[T]) *Dense[T]

NewDenseFromParams creates a Dense layer from existing Linear and Bias components. This is used for constructing layers from pre-existing parameters during model loading.

func (*Dense[T]) Attributes added in v0.2.0

func (d *Dense[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*Dense[T]) Backward

func (d *Dense[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients.

func (*Dense[T]) Forward

func (d *Dense[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the layer.

func (*Dense[T]) Name added in v0.2.1

func (d *Dense[T]) Name() string

Name returns the name of the Dense layer.

func (*Dense[T]) OpType added in v0.2.0

func (d *Dense[T]) OpType() string

OpType returns the operation type of the layer.

func (*Dense[T]) OutputShape

func (d *Dense[T]) OutputShape() []int

OutputShape returns the output shape of the layer.

func (*Dense[T]) Parameters

func (d *Dense[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer.

func (*Dense[T]) SetName

func (d *Dense[T]) SetName(name string)

SetName sets the name of the Dense layer.

type DenseOpt added in v0.2.1

type DenseOpt[T tensor.Numeric] func(*Dense[T])

DenseOpt is a functional option for configuring a Dense layer.

func WithActivation added in v0.2.1

func WithActivation[T tensor.Numeric](activation graph.Node[T]) DenseOpt[T]

WithActivation adds an activation function to the Dense layer.

func WithBias added in v0.2.0

func WithBias[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], outputFeatures int) DenseOpt[T]

WithBias adds a bias to the Dense layer. The bias is created eagerly; if creation fails the error is stored and surfaced by NewDense.

func WithoutBias added in v0.2.1

func WithoutBias[T tensor.Numeric]() DenseOpt[T]

WithoutBias disables bias for the Dense layer.

type Div added in v0.2.1

type Div[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Div represents an element-wise division node.

func NewDiv added in v0.2.1

func NewDiv[T tensor.Numeric](engine compute.Engine[T]) *Div[T]

NewDiv creates a new Div node.

func (*Div[T]) Attributes added in v0.2.1

func (d *Div[T]) Attributes() map[string]any

func (*Div[T]) Backward added in v0.2.1

func (d *Div[T]) Backward(ctx context.Context, _ types.BackwardMode, dOut *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Div[T]) Forward added in v0.2.1

func (d *Div[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Div[T]) OpType added in v0.2.1

func (d *Div[T]) OpType() string

func (*Div[T]) OutputShape added in v0.2.1

func (d *Div[T]) OutputShape() []int

func (*Div[T]) Parameters added in v0.2.1

func (d *Div[T]) Parameters() []*graph.Parameter[T]

type EAGLEHead added in v1.28.0

type EAGLEHead[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

EAGLEHead is a lightweight FFN that predicts the next hidden state from the penultimate transformer layer's hidden state. It is used in EAGLE-style self-speculative decoding to generate draft tokens without a separate model.

Architecture: LayerNorm -> Linear(hidden, hidden) -> SiLU -> Linear(hidden, hidden)

func NewEAGLEHead added in v1.28.0

func NewEAGLEHead[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	hiddenDim int,
) (*EAGLEHead[T], error)

NewEAGLEHead creates a new EAGLEHead layer with the given hidden dimension. The head maintains the same dimensionality throughout: hidden -> hidden.

func NewEAGLEHeadFromWeights added in v1.29.0

func NewEAGLEHeadFromWeights[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	w EAGLEHeadWeights[T],
) (*EAGLEHead[T], error)

NewEAGLEHeadFromWeights creates an EAGLEHead from pre-loaded weight tensors. This is used during GGUF model loading to construct the head with trained weights rather than random initialization.

func (*EAGLEHead[T]) Attributes added in v1.28.0

func (h *EAGLEHead[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*EAGLEHead[T]) Forward added in v1.28.0

func (h *EAGLEHead[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the EAGLE head prediction. Input shape: [batch, seq, hidden] -> Output shape: [batch, seq, hidden]

The computation is: LayerNorm -> Linear -> SiLU -> Linear where SiLU(x) = x * sigmoid(x).

func (*EAGLEHead[T]) OpType added in v1.28.0

func (h *EAGLEHead[T]) OpType() string

OpType returns the operation type.

func (*EAGLEHead[T]) OutputShape added in v1.28.0

func (h *EAGLEHead[T]) OutputShape() []int

OutputShape returns the output shape.

func (*EAGLEHead[T]) Parameters added in v1.28.0

func (h *EAGLEHead[T]) Parameters() []*graph.Parameter[T]

Parameters returns all trainable parameters of the EAGLE head.

type EAGLEHeadWeights added in v1.29.0

type EAGLEHeadWeights[T tensor.Numeric] struct {
	NormGamma *tensor.TensorNumeric[T] // LayerNorm scale, shape [hidden]
	NormBeta  *tensor.TensorNumeric[T] // LayerNorm shift, shape [hidden]
	FC1Weight *tensor.TensorNumeric[T] // First linear, shape [hidden, hidden]
	FC2Weight *tensor.TensorNumeric[T] // Second linear, shape [hidden, hidden]
}

EAGLEHeadWeights holds pre-loaded weight tensors for constructing an EAGLEHead from GGUF data. All tensors must have compatible shapes: fc1 and fc2 weights are [inputDim, outputDim], norm gamma and beta are [hiddenDim].

type Equal added in v0.2.1

type Equal[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Equal represents an element-wise equality comparison. Output is 1 for true, 0 for false.

func (*Equal[T]) Attributes added in v0.2.1

func (e *Equal[T]) Attributes() map[string]any

func (*Equal[T]) Backward added in v0.2.1

func (e *Equal[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Equal[T]) Forward added in v0.2.1

func (e *Equal[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Equal[T]) OpType added in v0.2.1

func (e *Equal[T]) OpType() string

func (*Equal[T]) OutputShape added in v0.2.1

func (e *Equal[T]) OutputShape() []int

func (*Equal[T]) Parameters added in v0.2.1

func (e *Equal[T]) Parameters() []*graph.Parameter[T]

type Expand added in v0.2.1

type Expand[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Expand broadcasts a tensor to a target shape.

func (*Expand[T]) Attributes added in v0.2.1

func (e *Expand[T]) Attributes() map[string]any

func (*Expand[T]) Backward added in v0.2.1

func (e *Expand[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Expand[T]) Forward added in v0.2.1

func (e *Expand[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Expand[T]) OpType added in v0.2.1

func (e *Expand[T]) OpType() string

func (*Expand[T]) OutputShape added in v0.2.1

func (e *Expand[T]) OutputShape() []int

func (*Expand[T]) Parameters added in v0.2.1

func (e *Expand[T]) Parameters() []*graph.Parameter[T]

type FFN added in v0.2.0

type FFN[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

FFN is a feed-forward network.

func NewFFN added in v0.2.0

func NewFFN[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputDim, hiddenDim, outputDim int,
	opts ...FFNOpt[T],
) (*FFN[T], error)

NewFFN creates a new FFN layer.

func NewFFNFromDense added in v1.36.0

func NewFFNFromDense[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	w1, w2, w3 *Dense[T],
	opts ...FFNOpt[T],
) (*FFN[T], error)

NewFFNFromDense constructs an FFN from pre-built Dense layers. Unlike NewFFN, this does NOT allocate random weight matrices — callers supply Dense layers that already reference pre-loaded weight tensors. This is the correct constructor for MoE expert FFNs where weights are sliced from a stacked tensor and must not trigger fresh allocations.

func (*FFN[T]) Attributes added in v0.2.0

func (f *FFN[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*FFN[T]) Backward added in v0.2.0

func (f *FFN[T]) Backward(ctx context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass of the FFN.

func (*FFN[T]) Forward added in v0.2.0

func (f *FFN[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the FFN.

func (*FFN[T]) MergedGateUpParameter added in v0.2.1

func (f *FFN[T]) MergedGateUpParameter() *graph.Parameter[T]

MergedGateUpParameter returns the merged gate+up parameter for GPU upload, or nil if not set.

func (*FFN[T]) OpType added in v0.2.0

func (f *FFN[T]) OpType() string

OpType returns the operation type of the layer.

func (*FFN[T]) OutputShape added in v0.2.0

func (f *FFN[T]) OutputShape() []int

OutputShape returns the output shape of the layer.

func (*FFN[T]) Parameters added in v0.2.0

func (f *FFN[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer.

func (*FFN[T]) SetMergedGateUp added in v0.2.1

func (f *FFN[T]) SetMergedGateUp(weight *tensor.TensorNumeric[T], gateDim, upDim int)

SetMergedGateUp sets a merged gate+up weight tensor for single-GEMV decode optimization. During decode (seqLen=1), a single MatMul replaces two separate gate and up projections.

type FFNConfig added in v0.2.1

type FFNConfig[T tensor.Numeric] struct {
}

FFNConfig holds configuration for FFN layers.

type FFNOpt added in v0.2.1

type FFNOpt[T tensor.Numeric] func(*FFN[T])

FFNOpt is a functional option for configuring a FFN layer.

func WithFFNNoBias added in v0.2.1

func WithFFNNoBias[T tensor.Numeric]() FFNOpt[T]

WithFFNNoBias disables bias for all layers in the FFN.

func WithSwiGLU added in v0.2.1

func WithSwiGLU[T tensor.Numeric]() FFNOpt[T]

WithSwiGLU enables SwiGLU activation.

type FiLM added in v0.2.1

type FiLM[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

FiLM (Feature-wise Linear Modulation) layer. It takes a feature tensor and a context tensor as input. It generates scale and bias vectors from the context tensor and applies them to the feature tensor. Output = (feature * scale) + bias

func NewFiLM added in v0.2.1

func NewFiLM[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	contextDim, featureDim int,
) (*FiLM[T], error)

NewFiLM creates a new FiLM layer.

func (*FiLM[T]) Attributes added in v0.2.1

func (f *FiLM[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the FiLM layer.

func (*FiLM[T]) Backward added in v0.2.1

func (f *FiLM[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the FiLM layer.

func (*FiLM[T]) Forward added in v0.2.1

func (f *FiLM[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass for the FiLM layer. It expects two inputs: inputs[0] is the feature tensor, inputs[1] is the context tensor.

func (*FiLM[T]) OpType added in v0.2.1

func (f *FiLM[T]) OpType() string

OpType returns the operation type of the FiLM layer.

func (*FiLM[T]) OutputShape added in v0.2.1

func (f *FiLM[T]) OutputShape() []int

OutputShape returns the output shape of the FiLM layer.

func (*FiLM[T]) Parameters added in v0.2.1

func (f *FiLM[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the FiLM layer.

type Gemm added in v0.2.1

type Gemm[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Gemm implements the ONNX Gemm operator: Y = alpha * A' * B' + beta * C where A' = transpose(A) if transA, B' = transpose(B) if transB.

func (*Gemm[T]) Attributes added in v0.2.1

func (g *Gemm[T]) Attributes() map[string]any

func (*Gemm[T]) Backward added in v0.2.1

func (g *Gemm[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Gemm[T]) Forward added in v0.2.1

func (g *Gemm[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Gemm[T]) OpType added in v0.2.1

func (g *Gemm[T]) OpType() string

func (*Gemm[T]) OutputShape added in v0.2.1

func (g *Gemm[T]) OutputShape() []int

func (*Gemm[T]) Parameters added in v0.2.1

func (g *Gemm[T]) Parameters() []*graph.Parameter[T]

type GlobalAveragePool added in v0.2.1

type GlobalAveragePool[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

GlobalAveragePool performs global average pooling over the spatial dimensions. Forward expects one 4D input [N, C, H, W] and produces [N, C, 1, 1].

func NewGlobalAveragePool added in v0.2.1

func NewGlobalAveragePool[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T]) *GlobalAveragePool[T]

NewGlobalAveragePool creates a GlobalAveragePool layer.

func (*GlobalAveragePool[T]) Attributes added in v0.2.1

func (g *GlobalAveragePool[T]) Attributes() map[string]interface{}

Attributes returns nil.

func (*GlobalAveragePool[T]) Backward added in v0.2.1

Backward returns nil (inference-only).

func (*GlobalAveragePool[T]) Forward added in v0.2.1

func (g *GlobalAveragePool[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes global average pooling.

func (*GlobalAveragePool[T]) OpType added in v0.2.1

func (g *GlobalAveragePool[T]) OpType() string

OpType returns "GlobalAveragePool".

func (*GlobalAveragePool[T]) OutputShape added in v0.2.1

func (g *GlobalAveragePool[T]) OutputShape() []int

OutputShape returns the output shape (populated after Forward).

func (*GlobalAveragePool[T]) Parameters added in v0.2.1

func (g *GlobalAveragePool[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil.

type Greater added in v0.2.1

type Greater[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Greater represents an element-wise greater-than comparison. Output is 1 for true, 0 for false.

func (*Greater[T]) Attributes added in v0.2.1

func (g *Greater[T]) Attributes() map[string]any

func (*Greater[T]) Backward added in v0.2.1

func (g *Greater[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Greater[T]) Forward added in v0.2.1

func (g *Greater[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Greater[T]) OpType added in v0.2.1

func (g *Greater[T]) OpType() string

func (*Greater[T]) OutputShape added in v0.2.1

func (g *Greater[T]) OutputShape() []int

func (*Greater[T]) Parameters added in v0.2.1

func (g *Greater[T]) Parameters() []*graph.Parameter[T]

type LMHead added in v0.2.0

type LMHead[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

LMHead is a linear layer that maps hidden states to vocabulary logits. When tiedWeight is set, it uses MatMulTransposeB to compute the projection directly without an explicit transpose, avoiding a 1GB+ temporary allocation and a use-after-free when the graph's ref-counting releases the transpose output.

func NewLMHead added in v0.2.0

func NewLMHead[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], hiddenDim, vocabSize int) (*LMHead[T], error)

NewLMHead creates a new LMHead.

func NewTiedLMHead added in v0.2.1

func NewTiedLMHead[T tensor.Numeric](engine compute.Engine[T], embedWeight *tensor.TensorNumeric[T]) *LMHead[T]

NewTiedLMHead creates an LMHead that reuses an existing embedding weight matrix (shape [vocabSize, hiddenDim]) instead of owning its own weight. Forward transposes the weight and performs the projection.

func (*LMHead[T]) Backward added in v0.2.0

func (h *LMHead[T]) Backward(ctx context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass of the LMHead.

func (*LMHead[T]) Forward added in v0.2.0

func (h *LMHead[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the LMHead.

func (*LMHead[T]) OutputShape added in v0.2.0

func (h *LMHead[T]) OutputShape() []int

OutputShape returns the output shape of the LMHead.

func (*LMHead[T]) Parameters added in v0.2.0

func (h *LMHead[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the LMHead. Tied LMHead returns nil since the embedding layer owns the weight.

func (*LMHead[T]) SetWeights added in v0.2.0

func (h *LMHead[T]) SetWeights(weights *tensor.TensorNumeric[T])

SetWeights sets the weights of the LMHead. This is useful for sharing weights with a token embedding layer.

type LessOrEqual added in v0.2.1

type LessOrEqual[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

LessOrEqual represents an element-wise less-than-or-equal comparison. Output is 1 for true, 0 for false.

func (*LessOrEqual[T]) Attributes added in v0.2.1

func (l *LessOrEqual[T]) Attributes() map[string]any

func (*LessOrEqual[T]) Backward added in v0.2.1

func (*LessOrEqual[T]) Forward added in v0.2.1

func (l *LessOrEqual[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*LessOrEqual[T]) OpType added in v0.2.1

func (l *LessOrEqual[T]) OpType() string

func (*LessOrEqual[T]) OutputShape added in v0.2.1

func (l *LessOrEqual[T]) OutputShape() []int

func (*LessOrEqual[T]) Parameters added in v0.2.1

func (l *LessOrEqual[T]) Parameters() []*graph.Parameter[T]

type Linear

type Linear[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Linear is a linear layer.

func NewLinear

func NewLinear[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputFeatures, outputFeatures int,
) (*Linear[T], error)

NewLinear creates a new Linear layer.

func NewLinearFromParam added in v0.2.0

func NewLinearFromParam[T tensor.Numeric](engine compute.Engine[T], param *graph.Parameter[T]) *Linear[T]

NewLinearFromParam creates a Linear layer from an existing parameter. This is used for constructing layers from pre-existing parameters during model loading.

func (*Linear[T]) Attributes added in v0.2.0

func (l *Linear[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*Linear[T]) Backward

func (l *Linear[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients.

func (*Linear[T]) Forward

func (l *Linear[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass of the layer.

func (*Linear[T]) Name added in v0.2.1

func (l *Linear[T]) Name() string

Name returns the name of the Linear layer.

func (*Linear[T]) OpType added in v0.2.0

func (l *Linear[T]) OpType() string

OpType returns the operation type of the layer.

func (*Linear[T]) OutputShape

func (l *Linear[T]) OutputShape() []int

OutputShape returns the output shape of the layer.

func (*Linear[T]) Parameters

func (l *Linear[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer.

func (*Linear[T]) SetName

func (l *Linear[T]) SetName(name string)

SetName sets the name of the Linear layer.

type MatMul added in v0.2.0

type MatMul[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

MatMul is a layer that performs matrix multiplication of two tensors.

func NewMatMul added in v0.2.0

func NewMatMul[T tensor.Numeric](engine compute.Engine[T]) *MatMul[T]

NewMatMul creates a new MatMul layer.

func (*MatMul[T]) Attributes added in v0.2.0

func (m *MatMul[T]) Attributes() map[string]interface{}

Attributes returns nil for the MatMul layer.

func (*MatMul[T]) Backward added in v0.2.0

func (m *MatMul[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the MatMul layer.

func (*MatMul[T]) Forward added in v0.2.0

func (m *MatMul[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the matrix multiplication.

func (*MatMul[T]) OpType added in v0.2.0

func (m *MatMul[T]) OpType() string

OpType returns the operation type of the MatMul layer.

func (*MatMul[T]) OutputShape added in v0.2.0

func (m *MatMul[T]) OutputShape() []int

OutputShape returns the output shape of the MatMul layer.

func (*MatMul[T]) Parameters added in v0.2.0

func (m *MatMul[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the MatMul layer.

type MatMulNBits added in v0.2.1

type MatMulNBits[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

MatMulNBits represents a matrix multiplication layer with N-bit quantized weights. This is commonly used for 4-bit quantized models like Gemma 3.

The layer performs: output = input @ dequantize(quantized_weights) Where dequantize unpacks N-bit weights and applies scale/zero-point transformation.

func NewMatMulNBits added in v0.2.1

func NewMatMulNBits[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	quantizedWeights *tensor.TensorNumeric[uint8],
	scale *tensor.TensorNumeric[T],
	zeroPoint *tensor.TensorNumeric[uint8],
	nbits int,
	symmetric bool,
) (*MatMulNBits[T], error)

NewMatMulNBits creates a new N-bit quantized matrix multiplication layer.

func (*MatMulNBits[T]) Attributes added in v0.2.1

func (m *MatMulNBits[T]) Attributes() map[string]interface{}

Attributes returns the node's non-tensor attributes.

func (*MatMulNBits[T]) Backward added in v0.2.1

func (m *MatMulNBits[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward performs the backward pass for MatMulNBits. Computes gradients with respect to input (weights are quantized and typically frozen).

func (*MatMulNBits[T]) Forward added in v0.2.1

func (m *MatMulNBits[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the forward pass: output = input @ dequantized_weights

func (*MatMulNBits[T]) GetDequantizedWeights added in v0.2.1

func (m *MatMulNBits[T]) GetDequantizedWeights() (*tensor.TensorNumeric[T], error)

GetDequantizedWeights returns the cached dequantized weights for inspection. Useful for debugging and testing.

func (*MatMulNBits[T]) InvalidateCache added in v0.2.1

func (m *MatMulNBits[T]) InvalidateCache()

InvalidateCache marks the dequantized weights cache as invalid. Call this if quantized weights, scale, or zero point are modified.

func (*MatMulNBits[T]) OpType added in v0.2.1

func (m *MatMulNBits[T]) OpType() string

OpType returns the operation type of the node.

func (*MatMulNBits[T]) OutputShape added in v0.2.1

func (m *MatMulNBits[T]) OutputShape() []int

OutputShape returns the shape of the output tensor.

func (*MatMulNBits[T]) Parameters added in v0.2.1

func (m *MatMulNBits[T]) Parameters() []*graph.Parameter[T]

Parameters returns the trainable parameters (empty for quantized layers).

func (*MatMulNBits[T]) QuantizationInfo added in v0.2.1

func (m *MatMulNBits[T]) QuantizationInfo() map[string]interface{}

QuantizationInfo returns information about the quantization configuration.

func (*MatMulNBits[T]) String added in v0.2.1

func (m *MatMulNBits[T]) String() string

String returns a string representation of the layer.

type Max added in v0.2.1

type Max[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Max computes the element-wise maximum of two tensors.

func (*Max[T]) Attributes added in v0.2.1

func (m *Max[T]) Attributes() map[string]any

func (*Max[T]) Backward added in v0.2.1

func (m *Max[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Max[T]) Forward added in v0.2.1

func (m *Max[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Max[T]) OpType added in v0.2.1

func (m *Max[T]) OpType() string

func (*Max[T]) OutputShape added in v0.2.1

func (m *Max[T]) OutputShape() []int

func (*Max[T]) Parameters added in v0.2.1

func (m *Max[T]) Parameters() []*graph.Parameter[T]

type MixtureOfExperts added in v0.2.1

type MixtureOfExperts[T tensor.Numeric] struct {
	SharedExpert graph.Node[T] // optional: runs on every token, output added to routed sum
	// contains filtered or unexported fields
}

MixtureOfExperts routes each token to topK experts selected by MoEGate and returns the weighted sum of expert outputs.

Forward expects exactly two inputs:

  • hiddenStates [seqLen, modelDim]
  • gateWeight [numExperts, modelDim]

Experts must be set at construction time as graph.Node[T] instances. Tech debt: ZMF sub-graph loading is not yet supported; experts are not populated by BuildMixtureOfExperts.

func NewMixtureOfExperts added in v0.2.1

func NewMixtureOfExperts[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	gate *MoEGate[T],
	experts []graph.Node[T],
	numExperts, topK int,
) *MixtureOfExperts[T]

NewMixtureOfExperts creates a MixtureOfExperts layer.

func (*MixtureOfExperts[T]) Attributes added in v0.2.1

func (m *MixtureOfExperts[T]) Attributes() map[string]any

Attributes returns the layer configuration.

func (*MixtureOfExperts[T]) Backward added in v0.2.1

func (m *MixtureOfExperts[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients for the MixtureOfExperts layer.

The forward computation is:

output[t] = sum_k(weights[t][k] * expert_k(hiddenStates[t]))

where weights come from softmax + top-K + normalize on router logits.

Backward pass:

  1. dX from experts: sum_k(weights[k] * dOut) — assuming identity-like expert gradients
  2. dWeights: for each selected expert k, dot(dOut[t], expert_k(x[t]))
  3. Route dWeights through the gate backward (softmax + STE for top-K)

Inputs must match forward: [hiddenStates, gateWeight]. Returns gradients [dHiddenStates, dGateWeight].

func (*MixtureOfExperts[T]) Forward added in v0.2.1

func (m *MixtureOfExperts[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward routes tokens to topK experts and returns the weighted sum [seqLen, modelDim]. For batch sizes > 1, tokens are grouped by expert and processed in batched forward calls instead of sequential per-token dispatch.

func (*MixtureOfExperts[T]) OpType added in v0.2.1

func (m *MixtureOfExperts[T]) OpType() string

OpType returns "MixtureOfExperts".

func (*MixtureOfExperts[T]) OutputShape added in v0.2.1

func (m *MixtureOfExperts[T]) OutputShape() []int

OutputShape returns the output shape from the last forward call.

func (*MixtureOfExperts[T]) Parameters added in v0.2.1

func (m *MixtureOfExperts[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil (no trainable parameters).

type MoEGate added in v0.2.1

type MoEGate[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

MoEGate computes sparse top-k expert routing for Mixture of Experts.

Forward expects exactly two inputs:

  • hiddenStates [seqLen, modelDim]
  • gateWeight [numExperts, modelDim]

Steps:

  1. logits = hiddenStates @ gateWeight.T -> [seqLen, numExperts]
  2. If routingBias is set, add bias to logits.
  3. probs = Softmax(logits, axis=1) or Sigmoid(logits) if sigmoidGating is enabled.
  4. For each token row: pick topK indices by descending probability.
  5. Normalize the topK scores so each row sums to 1.0.

Returns a [seqLen, topK] tensor of normalized expert weights.

func NewMoEGate added in v0.2.1

func NewMoEGate[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], topK int, opts ...MoEGateOption[T]) *MoEGate[T]

NewMoEGate creates a MoEGate layer with the given topK value and optional configuration.

func (*MoEGate[T]) Attributes added in v0.2.1

func (g *MoEGate[T]) Attributes() map[string]interface{}

Attributes returns the gate configuration.

func (*MoEGate[T]) Backward added in v0.2.1

func (g *MoEGate[T]) Backward(_ context.Context, _ types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients for the MoE gate using the straight-through estimator.

The top-K selection is a discrete operation. STE treats it as identity in the backward pass: gradients flow through the softmax as if all experts were selected, but only the top-K experts receive non-zero upstream gradient from the MoE output.

Inputs must match the forward call: [hiddenStates, gateWeight]. outputGradient has shape [seqLen, topK] — gradient w.r.t. the normalized gate weights.

Returns gradients [dHiddenStates, dGateWeight].

func (*MoEGate[T]) Forward added in v0.2.1

func (g *MoEGate[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward returns normalized expert weights shaped [seqLen, topK].

func (*MoEGate[T]) OpType added in v0.2.1

func (g *MoEGate[T]) OpType() string

OpType returns "MoEGate".

func (*MoEGate[T]) OutputShape added in v0.2.1

func (g *MoEGate[T]) OutputShape() []int

OutputShape returns the output shape from the last forward call.

func (*MoEGate[T]) Parameters added in v0.2.1

func (g *MoEGate[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil (no trainable parameters).

type MoEGateOption added in v1.33.0

type MoEGateOption[T tensor.Numeric] func(*MoEGate[T])

MoEGateOption configures optional MoEGate behavior.

func WithRoutingBias added in v1.33.0

func WithRoutingBias[T tensor.Numeric](bias *tensor.TensorNumeric[T]) MoEGateOption[T]

WithRoutingBias sets an optional bias tensor added to logits before gating. The bias shape must be [numExperts] and is broadcast across the sequence dimension.

func WithSigmoidGating added in v1.33.0

func WithSigmoidGating[T tensor.Numeric]() MoEGateOption[T]

WithSigmoidGating enables sigmoid gating instead of softmax for expert routing. When enabled, element-wise sigmoid is applied to logits instead of softmax.

type Mod added in v0.2.1

type Mod[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Mod computes element-wise remainder (fmod). Supports scalar broadcasting. ONNX Mod op with fmod=1 (default for floats).

func (*Mod[T]) Attributes added in v0.2.1

func (m *Mod[T]) Attributes() map[string]any

func (*Mod[T]) Backward added in v0.2.1

func (m *Mod[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Mod[T]) Forward added in v0.2.1

func (m *Mod[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Mod[T]) OpType added in v0.2.1

func (m *Mod[T]) OpType() string

func (*Mod[T]) OutputShape added in v0.2.1

func (m *Mod[T]) OutputShape() []int

func (*Mod[T]) Parameters added in v0.2.1

func (m *Mod[T]) Parameters() []*graph.Parameter[T]

type Mul added in v0.2.0

type Mul[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Mul is a layer that performs element-wise multiplication of two tensors.

func NewMul added in v0.2.0

func NewMul[T tensor.Numeric](engine compute.Engine[T]) *Mul[T]

NewMul creates a new Mul layer.

func (*Mul[T]) Attributes added in v0.2.0

func (m *Mul[T]) Attributes() map[string]interface{}

Attributes returns nil for the Mul layer.

func (*Mul[T]) Backward added in v0.2.0

func (m *Mul[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Mul layer.

func (*Mul[T]) Forward added in v0.2.0

func (m *Mul[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the element-wise multiplication of two input tensors.

func (*Mul[T]) OpType added in v0.2.0

func (m *Mul[T]) OpType() string

OpType returns the operation type of the Mul layer.

func (*Mul[T]) OutputShape added in v0.2.0

func (m *Mul[T]) OutputShape() []int

OutputShape returns the output shape of the Mul layer.

func (*Mul[T]) Parameters added in v0.2.0

func (m *Mul[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Mul layer.

type Neg added in v0.2.1

type Neg[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Neg represents an element-wise negation node.

func (*Neg[T]) Attributes added in v0.2.1

func (n *Neg[T]) Attributes() map[string]any

func (*Neg[T]) Backward added in v0.2.1

func (n *Neg[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Neg[T]) Forward added in v0.2.1

func (n *Neg[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Neg[T]) OpType added in v0.2.1

func (n *Neg[T]) OpType() string

func (*Neg[T]) OutputShape added in v0.2.1

func (n *Neg[T]) OutputShape() []int

func (*Neg[T]) Parameters added in v0.2.1

func (n *Neg[T]) Parameters() []*graph.Parameter[T]

type Or added in v0.2.1

type Or[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Or represents an element-wise logical OR. Output is 1 when either input is nonzero, 0 otherwise.

func (*Or[T]) Attributes added in v0.2.1

func (o *Or[T]) Attributes() map[string]any

func (*Or[T]) Backward added in v0.2.1

func (o *Or[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Or[T]) Forward added in v0.2.1

func (o *Or[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Or[T]) OpType added in v0.2.1

func (o *Or[T]) OpType() string

func (*Or[T]) OutputShape added in v0.2.1

func (o *Or[T]) OutputShape() []int

func (*Or[T]) Parameters added in v0.2.1

func (o *Or[T]) Parameters() []*graph.Parameter[T]

type Pad added in v0.2.1

type Pad[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Pad applies constant-value padding to a tensor. pads has shape [2*ndim]: [begin_0, begin_1, ..., end_0, end_1, ...].

func NewPad added in v0.2.1

func NewPad[T tensor.Numeric](engine compute.Engine[T], pads []int64, constantValue T) *Pad[T]

NewPad creates a new Pad layer.

func (*Pad[T]) Attributes added in v0.2.1

func (p *Pad[T]) Attributes() map[string]interface{}

Attributes returns the pad configuration.

func (*Pad[T]) Backward added in v0.2.1

func (p *Pad[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward returns nil (not required for inference).

func (*Pad[T]) Forward added in v0.2.1

func (p *Pad[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward pads the input tensor with the configured constant value.

func (*Pad[T]) OpType added in v0.2.1

func (p *Pad[T]) OpType() string

OpType returns "Pad".

func (*Pad[T]) OutputShape added in v0.2.1

func (p *Pad[T]) OutputShape() []int

OutputShape returns the output shape from the last forward call.

func (*Pad[T]) Parameters added in v0.2.1

func (p *Pad[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil (no trainable parameters).

type PolynomialExpansion

type PolynomialExpansion[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

PolynomialExpansion layer transforms input features into polynomial combinations up to a specified degree. This is useful for capturing non-linear relationships in data through feature engineering.

For input [x1, x2] with degree 2, it generates: [1, x1, x2, x1^2, x1*x2, x2^2]

The layer supports: - Configurable polynomial degree - Optional bias term (constant 1) - Interaction terms between features - Efficient computation using tensor operations.

func NewPolynomialExpansion

func NewPolynomialExpansion[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	inputSize int,
	options ...PolynomialExpansionOption[T],
) (*PolynomialExpansion[T], error)

NewPolynomialExpansion creates a new polynomial expansion layer.

Parameters: - name: layer name (currently not used but kept for consistency) - engine: compute engine for tensor operations - ops: arithmetic operations for the numeric type - inputSize: number of input features - options: functional options for configuration

Default values: - degree: 2 - includeBias: true

Returns the polynomial expansion layer or an error if parameters are invalid.

func (*PolynomialExpansion[T]) Attributes added in v0.2.0

func (p *PolynomialExpansion[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the PolynomialExpansion layer.

func (*PolynomialExpansion[T]) Backward

func (p *PolynomialExpansion[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients for the polynomial expansion layer. This computes the derivative of each polynomial term with respect to the input features.

func (*PolynomialExpansion[T]) Forward

func (p *PolynomialExpansion[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward performs the polynomial expansion transformation. Input shape: [batch_size, input_size] Output shape: [batch_size, output_size].

func (*PolynomialExpansion[T]) GetDegree

func (p *PolynomialExpansion[T]) GetDegree() int

GetDegree returns the polynomial degree of the layer.

func (*PolynomialExpansion[T]) GetInputSize

func (p *PolynomialExpansion[T]) GetInputSize() int

GetInputSize returns the input size of the layer.

func (*PolynomialExpansion[T]) GetOutputSize

func (p *PolynomialExpansion[T]) GetOutputSize() int

GetOutputSize returns the output size of the layer.

func (*PolynomialExpansion[T]) GetTermIndices

func (p *PolynomialExpansion[T]) GetTermIndices() [][]int

GetTermIndices returns the polynomial term indices for inspection/debugging.

func (*PolynomialExpansion[T]) HasBias

func (p *PolynomialExpansion[T]) HasBias() bool

HasBias returns whether the layer includes a bias term.

func (*PolynomialExpansion[T]) OpType added in v0.2.0

func (p *PolynomialExpansion[T]) OpType() string

OpType returns the operation type of the PolynomialExpansion layer.

func (*PolynomialExpansion[T]) OutputShape

func (p *PolynomialExpansion[T]) OutputShape() []int

OutputShape returns the shape of the output tensor.

func (*PolynomialExpansion[T]) Parameters

func (p *PolynomialExpansion[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer. Polynomial expansion has no trainable parameters.

func (*PolynomialExpansion[T]) SetName

func (p *PolynomialExpansion[T]) SetName(_ string)

SetName sets the name of the layer (for consistency with other layers). SetName sets the name of the layer (for consistency with other layers).

type PolynomialExpansionOption added in v0.2.0

type PolynomialExpansionOption[T tensor.Numeric] func(*PolynomialExpansionOptions[T])

PolynomialExpansionOption is a function that configures PolynomialExpansionOptions.

func WithPolynomialBias added in v0.2.0

func WithPolynomialBias[T tensor.Numeric](includeBias bool) PolynomialExpansionOption[T]

WithPolynomialBias sets whether to include a bias term (constant 1).

func WithPolynomialDegree added in v0.2.0

func WithPolynomialDegree[T tensor.Numeric](degree int) PolynomialExpansionOption[T]

WithPolynomialDegree sets the maximum polynomial degree.

type PolynomialExpansionOptions added in v0.2.0

type PolynomialExpansionOptions[T tensor.Numeric] struct {
	Degree      int
	IncludeBias bool
}

PolynomialExpansionOptions holds configuration options for PolynomialExpansion layer.

type PoolMode added in v1.9.0

type PoolMode int

PoolMode determines how token hidden states are aggregated into a fixed-size representation before classification.

const (
	// PoolCLS takes the hidden state of the first token (index 0) as the
	// sequence representation. This is the standard approach for BERT-style
	// models where a [CLS] token is prepended.
	PoolCLS PoolMode = iota

	// PoolMean computes the mean of all token hidden states across the
	// sequence dimension to produce the sequence representation.
	PoolMean
)

type Pow added in v0.2.1

type Pow[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Pow represents an element-wise power node.

func NewPow added in v0.2.1

func NewPow[T tensor.Numeric](engine compute.Engine[T]) *Pow[T]

NewPow creates a new Pow node.

func (*Pow[T]) Attributes added in v0.2.1

func (p *Pow[T]) Attributes() map[string]any

func (*Pow[T]) Backward added in v0.2.1

func (p *Pow[T]) Backward(ctx context.Context, _ types.BackwardMode, dOut *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Pow[T]) Forward added in v0.2.1

func (p *Pow[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Pow[T]) OpType added in v0.2.1

func (p *Pow[T]) OpType() string

func (*Pow[T]) OutputShape added in v0.2.1

func (p *Pow[T]) OutputShape() []int

func (*Pow[T]) Parameters added in v0.2.1

func (p *Pow[T]) Parameters() []*graph.Parameter[T]

type Range added in v0.2.1

type Range[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Range generates a sequence of numbers [start, limit) with given delta.

func (*Range[T]) Attributes added in v0.2.1

func (r *Range[T]) Attributes() map[string]any

func (*Range[T]) Backward added in v0.2.1

func (r *Range[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Range[T]) Forward added in v0.2.1

func (r *Range[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Range[T]) OpType added in v0.2.1

func (r *Range[T]) OpType() string

func (*Range[T]) OutputShape added in v0.2.1

func (r *Range[T]) OutputShape() []int

func (*Range[T]) Parameters added in v0.2.1

func (r *Range[T]) Parameters() []*graph.Parameter[T]

type ReduceMean added in v0.2.1

type ReduceMean[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

ReduceMean reduces a tensor by computing the mean along specified axes.

func (*ReduceMean[T]) Attributes added in v0.2.1

func (r *ReduceMean[T]) Attributes() map[string]any

func (*ReduceMean[T]) Backward added in v0.2.1

func (*ReduceMean[T]) Forward added in v0.2.1

func (r *ReduceMean[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*ReduceMean[T]) OpType added in v0.2.1

func (r *ReduceMean[T]) OpType() string

func (*ReduceMean[T]) OutputShape added in v0.2.1

func (r *ReduceMean[T]) OutputShape() []int

func (*ReduceMean[T]) Parameters added in v0.2.1

func (r *ReduceMean[T]) Parameters() []*graph.Parameter[T]

type Reshape added in v0.2.0

type Reshape[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Reshape is a layer that changes the shape of a tensor without changing its data.

func NewReshape added in v0.2.0

func NewReshape[T tensor.Numeric](engine compute.Engine[T], targetShape []int) *Reshape[T]

NewReshape creates a new Reshape layer.

func (*Reshape[T]) Attributes added in v0.2.0

func (r *Reshape[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Reshape layer.

func (*Reshape[T]) Backward added in v0.2.0

func (r *Reshape[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Reshape layer.

func (*Reshape[T]) Forward added in v0.2.0

func (r *Reshape[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the reshape operation. Accepts 1 input (static target shape from attributes) or 2 inputs (ONNX opset 5+: data + shape tensor). In the shape tensor, -1 means infer and 0 means copy from the corresponding input dimension.

func (*Reshape[T]) OpType added in v0.2.0

func (r *Reshape[T]) OpType() string

OpType returns the operation type of the Reshape layer.

func (*Reshape[T]) OutputShape added in v0.2.0

func (r *Reshape[T]) OutputShape() []int

OutputShape returns the output shape of the Reshape layer.

func (*Reshape[T]) Parameters added in v0.2.0

func (r *Reshape[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Reshape layer.

type Resize added in v0.2.1

type Resize[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Resize resamples a 4D input tensor to a new spatial size. Only nearest-neighbor interpolation is implemented. Either scales or sizes (but not both) must be provided at build time. Forward expects exactly one input: X [N, C, H, W].

func NewResize added in v0.2.1

func NewResize[T tensor.Numeric](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	mode string,
	scales []float64,
	sizes []int64,
) *Resize[T]

NewResize creates a Resize layer. Provide either non-nil scales or non-nil sizes (not both).

func (*Resize[T]) Attributes added in v0.2.1

func (r *Resize[T]) Attributes() map[string]interface{}

Attributes returns the resize configuration.

func (*Resize[T]) Backward added in v0.2.1

func (r *Resize[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward returns nil (inference-only).

func (*Resize[T]) Forward added in v0.2.1

func (r *Resize[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward resamples X to the target spatial size.

func (*Resize[T]) OpType added in v0.2.1

func (r *Resize[T]) OpType() string

OpType returns "Resize".

func (*Resize[T]) OutputShape added in v0.2.1

func (r *Resize[T]) OutputShape() []int

OutputShape returns the output shape (populated after Forward).

func (*Resize[T]) Parameters added in v0.2.1

func (r *Resize[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil.

type RotaryEmbedding added in v0.2.0

type RotaryEmbedding[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

RotaryEmbedding applies rotary position embedding to input tensors.

func NewRotaryEmbedding added in v0.2.0

func NewRotaryEmbedding[T tensor.Numeric](engine compute.Engine[T]) *RotaryEmbedding[T]

NewRotaryEmbedding creates a new RotaryEmbedding layer.

func (*RotaryEmbedding[T]) Attributes added in v0.2.0

func (r *RotaryEmbedding[T]) Attributes() map[string]interface{}

Attributes returns nil for the RotaryEmbedding layer.

func (*RotaryEmbedding[T]) Backward added in v0.2.0

func (r *RotaryEmbedding[T]) Backward(_ context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the RotaryEmbedding layer.

func (*RotaryEmbedding[T]) Forward added in v0.2.0

func (r *RotaryEmbedding[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward applies rotary embedding to the input.

func (*RotaryEmbedding[T]) Name added in v0.2.0

func (r *RotaryEmbedding[T]) Name() string

Name returns the name of the layer.

func (*RotaryEmbedding[T]) OpType added in v0.2.0

func (r *RotaryEmbedding[T]) OpType() string

OpType returns the operation type of the RotaryEmbedding layer.

func (*RotaryEmbedding[T]) OutputShape added in v0.2.0

func (r *RotaryEmbedding[T]) OutputShape() []int

OutputShape returns the output shape of the layer.

func (*RotaryEmbedding[T]) Parameters added in v0.2.0

func (r *RotaryEmbedding[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer (none for RotaryEmbedding).

func (*RotaryEmbedding[T]) SetName added in v0.2.0

func (r *RotaryEmbedding[T]) SetName(name string)

SetName sets the name of the layer.

type ScatterND added in v0.2.1

type ScatterND[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

ScatterND scatters updates into a copy of the data tensor at indices.

func (*ScatterND[T]) Attributes added in v0.2.1

func (s *ScatterND[T]) Attributes() map[string]any

func (*ScatterND[T]) Backward added in v0.2.1

func (*ScatterND[T]) Forward added in v0.2.1

func (s *ScatterND[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*ScatterND[T]) OpType added in v0.2.1

func (s *ScatterND[T]) OpType() string

func (*ScatterND[T]) OutputShape added in v0.2.1

func (s *ScatterND[T]) OutputShape() []int

func (*ScatterND[T]) Parameters added in v0.2.1

func (s *ScatterND[T]) Parameters() []*graph.Parameter[T]

type SeqClassification added in v1.9.0

type SeqClassification[T tensor.Float] struct {
	// contains filtered or unexported fields
}

SeqClassification is a sequence classification head that pools token-level hidden states into a fixed-size vector and projects it to class logits.

Forward expects input of shape [batch, seqLen, hiddenDim] and produces output of shape [batch, numClasses].

func NewSeqClassification added in v1.9.0

func NewSeqClassification[T tensor.Float](
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	poolMode PoolMode,
	hiddenDim, numClasses int,
	opts ...SeqClassificationOpt[T],
) (*SeqClassification[T], error)

NewSeqClassification creates a new sequence classification head.

func (*SeqClassification[T]) Attributes added in v1.9.0

func (s *SeqClassification[T]) Attributes() map[string]interface{}

Attributes returns the layer attributes.

func (*SeqClassification[T]) Backward added in v1.9.0

func (s *SeqClassification[T]) Backward(ctx context.Context, mode types.BackwardMode, dOut *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the backward pass.

func (*SeqClassification[T]) Forward added in v1.9.0

func (s *SeqClassification[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the forward pass. Input shape: [batch, seqLen, hiddenDim] -> Output shape: [batch, numClasses].

func (*SeqClassification[T]) OpType added in v1.9.0

func (s *SeqClassification[T]) OpType() string

OpType returns "SeqClassification".

func (*SeqClassification[T]) OutputShape added in v1.9.0

func (s *SeqClassification[T]) OutputShape() []int

OutputShape returns the output shape from the most recent Forward call.

func (*SeqClassification[T]) Parameters added in v1.9.0

func (s *SeqClassification[T]) Parameters() []*graph.Parameter[T]

Parameters returns the trainable parameters (from the Dense layer).

func (*SeqClassification[T]) SetTraining added in v1.9.0

func (s *SeqClassification[T]) SetTraining(training bool)

SetTraining enables or disables training mode on the dropout sub-layer.

type SeqClassificationOpt added in v1.9.0

type SeqClassificationOpt[T tensor.Float] func(*SeqClassification[T])

SeqClassificationOpt is a functional option for configuring a SeqClassification layer.

func WithDropout added in v1.9.0

func WithDropout[T tensor.Float](engine compute.Engine[T], ops numeric.Arithmetic[T], rate T) SeqClassificationOpt[T]

WithDropout adds dropout before the linear projection with the given rate.

type Shape added in v0.2.0

type Shape[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Shape is a layer that outputs the shape of its input tensor.

func New added in v0.2.0

func New[T tensor.Numeric](engine compute.Engine[T]) *Shape[T]

New creates a new Shape layer.

func (*Shape[T]) Attributes added in v0.2.0

func (s *Shape[T]) Attributes() map[string]interface{}

Attributes returns nil for the Shape layer.

func (*Shape[T]) Backward added in v0.2.0

func (s *Shape[T]) Backward(_ context.Context, mode types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Shape layer.

func (*Shape[T]) Forward added in v0.2.0

func (s *Shape[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the shape of the input tensor.

func (*Shape[T]) OpType added in v0.2.0

func (s *Shape[T]) OpType() string

OpType returns the operation type of the Shape layer.

func (*Shape[T]) OutputShape added in v0.2.0

func (s *Shape[T]) OutputShape() []int

OutputShape returns the output shape of the Shape layer.

func (*Shape[T]) Parameters added in v0.2.0

func (s *Shape[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Shape layer.

type Sin added in v0.2.1

type Sin[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Sin represents an element-wise sine node.

func (*Sin[T]) Attributes added in v0.2.1

func (s *Sin[T]) Attributes() map[string]any

func (*Sin[T]) Backward added in v0.2.1

func (s *Sin[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Sin[T]) Forward added in v0.2.1

func (s *Sin[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Sin[T]) OpType added in v0.2.1

func (s *Sin[T]) OpType() string

func (*Sin[T]) OutputShape added in v0.2.1

func (s *Sin[T]) OutputShape() []int

func (*Sin[T]) Parameters added in v0.2.1

func (s *Sin[T]) Parameters() []*graph.Parameter[T]

type Slice added in v0.2.1

type Slice[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Slice extracts a sub-tensor using start/end/axes/steps attributes. Steps other than 1 are not supported; steps are accepted but ignored.

func NewSlice added in v0.2.1

func NewSlice[T tensor.Numeric](engine compute.Engine[T], starts, ends, axes, steps []int64) *Slice[T]

NewSlice creates a new Slice layer.

func (*Slice[T]) Attributes added in v0.2.1

func (s *Slice[T]) Attributes() map[string]interface{}

Attributes returns the slice configuration.

func (*Slice[T]) Backward added in v0.2.1

func (s *Slice[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward returns nil (not required for inference).

func (*Slice[T]) Forward added in v0.2.1

func (s *Slice[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward applies the slice operation to the input tensor. Accepts 1 input (attribute-based, opset 1-9) or 3-5 inputs (ONNX opset 10+: data, starts, ends, [axes], [steps]).

func (*Slice[T]) OpType added in v0.2.1

func (s *Slice[T]) OpType() string

OpType returns "Slice".

func (*Slice[T]) OutputShape added in v0.2.1

func (s *Slice[T]) OutputShape() []int

OutputShape returns the output shape from the last forward call.

func (*Slice[T]) Parameters added in v0.2.1

func (s *Slice[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil (no trainable parameters).

type SpectralFingerprint added in v0.2.1

type SpectralFingerprint[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

SpectralFingerprint implements a layer that extracts the top-K frequency components from the input using FFT, creating a fixed-size spectral signature for variable-length inputs.

func NewSpectralFingerprint added in v0.2.1

func NewSpectralFingerprint[T tensor.Numeric](engine compute.Engine[T], ops numeric.Arithmetic[T], window, topK int) (*SpectralFingerprint[T], error)

NewSpectralFingerprint creates a new SpectralFingerprint layer.

func (*SpectralFingerprint[T]) Attributes added in v0.2.1

func (s *SpectralFingerprint[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*SpectralFingerprint[T]) Backward added in v0.2.1

Backward returns no gradients (treated as non-differentiable feature transform).

func (*SpectralFingerprint[T]) Forward added in v0.2.1

func (s *SpectralFingerprint[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes spectral magnitudes for bins 1..topK for each row in the batch. Input must be [batch, window]. If input window dimension is larger than the configured window, only the last `window` elements are used. If smaller, an error is returned.

func (*SpectralFingerprint[T]) OpType added in v0.2.1

func (s *SpectralFingerprint[T]) OpType() string

OpType returns the operation type.

func (*SpectralFingerprint[T]) OutputShape added in v0.2.1

func (s *SpectralFingerprint[T]) OutputShape() []int

OutputShape returns the last computed output shape.

func (*SpectralFingerprint[T]) Parameters added in v0.2.1

func (s *SpectralFingerprint[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for SpectralFingerprint.

type Sqrt added in v0.2.1

type Sqrt[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Sqrt represents an element-wise square root node.

func NewSqrt added in v0.2.1

func NewSqrt[T tensor.Numeric](engine compute.Engine[T]) *Sqrt[T]

NewSqrt creates a new Sqrt node.

func (*Sqrt[T]) Attributes added in v0.2.1

func (s *Sqrt[T]) Attributes() map[string]any

func (*Sqrt[T]) Backward added in v0.2.1

func (s *Sqrt[T]) Backward(ctx context.Context, _ types.BackwardMode, dOut *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Sqrt[T]) Forward added in v0.2.1

func (s *Sqrt[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Sqrt[T]) OpType added in v0.2.1

func (s *Sqrt[T]) OpType() string

func (*Sqrt[T]) OutputShape added in v0.2.1

func (s *Sqrt[T]) OutputShape() []int

func (*Sqrt[T]) Parameters added in v0.2.1

func (s *Sqrt[T]) Parameters() []*graph.Parameter[T]

type Squeeze added in v0.2.1

type Squeeze[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Squeeze removes dimensions of size 1 at the specified axes. If no axes are specified, all size-1 dimensions are removed.

func (*Squeeze[T]) Attributes added in v0.2.1

func (s *Squeeze[T]) Attributes() map[string]any

func (*Squeeze[T]) Backward added in v0.2.1

func (s *Squeeze[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Squeeze[T]) Forward added in v0.2.1

func (s *Squeeze[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Squeeze[T]) OpType added in v0.2.1

func (s *Squeeze[T]) OpType() string

func (*Squeeze[T]) OutputShape added in v0.2.1

func (s *Squeeze[T]) OutputShape() []int

func (*Squeeze[T]) Parameters added in v0.2.1

func (s *Squeeze[T]) Parameters() []*graph.Parameter[T]

type Sub added in v0.2.0

type Sub[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Sub is a layer that performs element-wise subtraction of two tensors.

func NewSub added in v0.2.0

func NewSub[T tensor.Numeric](engine compute.Engine[T]) *Sub[T]

NewSub creates a new Sub layer.

func (*Sub[T]) Attributes added in v0.2.0

func (s *Sub[T]) Attributes() map[string]interface{}

Attributes returns nil for the Sub layer.

func (*Sub[T]) Backward added in v0.2.0

func (s *Sub[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Sub layer.

func (*Sub[T]) Forward added in v0.2.0

func (s *Sub[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the element-wise subtraction of two input tensors (a - b).

func (*Sub[T]) OpType added in v0.2.0

func (s *Sub[T]) OpType() string

OpType returns the operation type of the Sub layer.

func (*Sub[T]) OutputShape added in v0.2.0

func (s *Sub[T]) OutputShape() []int

OutputShape returns the output shape of the Sub layer.

func (*Sub[T]) Parameters added in v0.2.0

func (s *Sub[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Sub layer.

type TemporalConvEncoder added in v0.2.1

type TemporalConvEncoder[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

TemporalConvEncoder replaces the non-differentiable SpectralFingerprint. Two stacked Conv1D layers with ReLU + global average pool + linear projection.

Input: [batch, numStats, windowSize] (per-era group stats over time) Output: [batch, outputDim]

Pipeline:

conv1 = Conv1D(numStats, hiddenChannels, kernel=3, pad=1) + ReLU
conv2 = Conv1D(hiddenChannels, hiddenChannels, kernel=3, pad=1) + ReLU
pooled = ReduceMean(conv2_out, axis=2)  -- global average pool
output = Linear(hiddenChannels, outputDim)

func NewTemporalConvEncoder added in v0.2.1

func NewTemporalConvEncoder[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	numStats, hiddenChannels, outputDim int,
) (*TemporalConvEncoder[T], error)

NewTemporalConvEncoder creates a new TemporalConvEncoder.

func (*TemporalConvEncoder[T]) Attributes added in v0.2.1

func (t *TemporalConvEncoder[T]) Attributes() map[string]interface{}

func (*TemporalConvEncoder[T]) Backward added in v0.2.1

func (te *TemporalConvEncoder[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients through the encoder.

func (*TemporalConvEncoder[T]) Forward added in v0.2.1

func (te *TemporalConvEncoder[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward processes temporal group statistics through conv layers.

func (*TemporalConvEncoder[T]) OpType added in v0.2.1

func (t *TemporalConvEncoder[T]) OpType() string

func (*TemporalConvEncoder[T]) OutputShape added in v0.2.1

func (t *TemporalConvEncoder[T]) OutputShape() []int

func (*TemporalConvEncoder[T]) Parameters added in v0.2.1

func (te *TemporalConvEncoder[T]) Parameters() []*graph.Parameter[T]

Parameters returns all trainable parameters.

type Tile added in v0.2.1

type Tile[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Tile repeats a tensor along each dimension according to the repeats tensor. ONNX Tile op: output[i] = input[i % input_shape[dim]] for each dim.

func (*Tile[T]) Attributes added in v0.2.1

func (t *Tile[T]) Attributes() map[string]any

func (*Tile[T]) Backward added in v0.2.1

func (t *Tile[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Tile[T]) Forward added in v0.2.1

func (t *Tile[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Tile[T]) OpType added in v0.2.1

func (t *Tile[T]) OpType() string

func (*Tile[T]) OutputShape added in v0.2.1

func (t *Tile[T]) OutputShape() []int

func (*Tile[T]) Parameters added in v0.2.1

func (t *Tile[T]) Parameters() []*graph.Parameter[T]

type TopK added in v0.2.1

type TopK[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

TopK selects the top-k values along the last axis of the input tensor. It returns only the values (not indices) as a single output tensor. When largest=true (default) it returns the k largest values in descending order; when largest=false it returns the k smallest values in ascending order.

func NewTopK added in v0.2.1

func NewTopK[T tensor.Numeric](engine compute.Engine[T], k, axis int, largest, sorted bool) *TopK[T]

NewTopK creates a new TopK layer.

func (*TopK[T]) Attributes added in v0.2.1

func (tk *TopK[T]) Attributes() map[string]interface{}

Attributes returns the TopK configuration.

func (*TopK[T]) Backward added in v0.2.1

func (tk *TopK[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward returns nil (not required for inference).

func (*TopK[T]) Forward added in v0.2.1

func (tk *TopK[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward selects the top-k values along the last axis of the input.

func (*TopK[T]) OpType added in v0.2.1

func (tk *TopK[T]) OpType() string

OpType returns "TopK".

func (*TopK[T]) OutputShape added in v0.2.1

func (tk *TopK[T]) OutputShape() []int

OutputShape returns the output shape from the last forward call.

func (*TopK[T]) Parameters added in v0.2.1

func (tk *TopK[T]) Parameters() []*graph.Parameter[T]

Parameters returns nil (no trainable parameters).

type Trilu added in v0.2.1

type Trilu[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Trilu extracts the upper or lower triangular part of a 2D matrix (or batch of matrices).

func (*Trilu[T]) Attributes added in v0.2.1

func (t *Trilu[T]) Attributes() map[string]any

func (*Trilu[T]) Backward added in v0.2.1

func (tr *Trilu[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Trilu[T]) Forward added in v0.2.1

func (tr *Trilu[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Trilu[T]) OpType added in v0.2.1

func (t *Trilu[T]) OpType() string

func (*Trilu[T]) OutputShape added in v0.2.1

func (t *Trilu[T]) OutputShape() []int

func (*Trilu[T]) Parameters added in v0.2.1

func (t *Trilu[T]) Parameters() []*graph.Parameter[T]

type Unsqueeze added in v0.2.0

type Unsqueeze[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Unsqueeze is a layer that adds dimensions of size 1 to a tensor at specified axes.

func NewUnsqueeze added in v0.2.0

func NewUnsqueeze[T tensor.Numeric](engine compute.Engine[T], axes []int) *Unsqueeze[T]

NewUnsqueeze creates a new Unsqueeze layer.

func (*Unsqueeze[T]) Attributes added in v0.2.0

func (u *Unsqueeze[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the Unsqueeze layer.

func (*Unsqueeze[T]) Backward added in v0.2.0

func (u *Unsqueeze[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes the gradients for the Unsqueeze layer.

func (*Unsqueeze[T]) Forward added in v0.2.0

func (u *Unsqueeze[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes the unsqueeze operation by reshaping the input tensor.

func (*Unsqueeze[T]) OpType added in v0.2.0

func (u *Unsqueeze[T]) OpType() string

OpType returns the operation type of the Unsqueeze layer.

func (*Unsqueeze[T]) OutputShape added in v0.2.0

func (u *Unsqueeze[T]) OutputShape() []int

OutputShape returns the output shape of the Unsqueeze layer.

func (*Unsqueeze[T]) Parameters added in v0.2.0

func (u *Unsqueeze[T]) Parameters() []*graph.Parameter[T]

Parameters returns no trainable parameters for the Unsqueeze layer.

type VariableSelection added in v0.2.1

type VariableSelection[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

VariableSelection implements per-sample soft feature gating. Given input [batch, num_features]:

hidden = GELU(input @ W1 + b1)          [batch, hidden_dim]
logits = hidden @ W2 + b2               [batch, num_features]
weights = Softmax(logits, axis=1)        [batch, num_features]
output = input * weights * num_features  [batch, num_features]

The rescaling by num_features preserves expected magnitude.

func NewVariableSelection added in v0.2.1

func NewVariableSelection[T tensor.Numeric](
	name string,
	engine compute.Engine[T],
	ops numeric.Arithmetic[T],
	numFeatures, hiddenDim int,
) (*VariableSelection[T], error)

NewVariableSelection creates a new VariableSelection layer.

func (*VariableSelection[T]) Attributes added in v0.2.1

func (v *VariableSelection[T]) Attributes() map[string]interface{}

Attributes returns the attributes of the layer.

func (*VariableSelection[T]) Backward added in v0.2.1

func (v *VariableSelection[T]) Backward(ctx context.Context, mode types.BackwardMode, outputGradient *tensor.TensorNumeric[T], inputs ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

Backward computes gradients for VariableSelection.

func (*VariableSelection[T]) Forward added in v0.2.1

func (v *VariableSelection[T]) Forward(ctx context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

Forward computes per-sample feature gating.

func (*VariableSelection[T]) OpType added in v0.2.1

func (v *VariableSelection[T]) OpType() string

OpType returns the operation type of the layer.

func (*VariableSelection[T]) OutputShape added in v0.2.1

func (v *VariableSelection[T]) OutputShape() []int

OutputShape returns the output shape of the layer.

func (*VariableSelection[T]) Parameters added in v0.2.1

func (v *VariableSelection[T]) Parameters() []*graph.Parameter[T]

Parameters returns the parameters of the layer.

type Where added in v0.2.1

type Where[T tensor.Numeric] struct {
	// contains filtered or unexported fields
}

Where selects elements from two tensors based on a condition tensor. Output[i] = x[i] if condition[i] != 0, else y[i].

func (*Where[T]) Attributes added in v0.2.1

func (w *Where[T]) Attributes() map[string]any

func (*Where[T]) Backward added in v0.2.1

func (w *Where[T]) Backward(_ context.Context, _ types.BackwardMode, _ *tensor.TensorNumeric[T], _ ...*tensor.TensorNumeric[T]) ([]*tensor.TensorNumeric[T], error)

func (*Where[T]) Forward added in v0.2.1

func (w *Where[T]) Forward(_ context.Context, inputs ...*tensor.TensorNumeric[T]) (*tensor.TensorNumeric[T], error)

func (*Where[T]) OpType added in v0.2.1

func (w *Where[T]) OpType() string

func (*Where[T]) OutputShape added in v0.2.1

func (w *Where[T]) OutputShape() []int

func (*Where[T]) Parameters added in v0.2.1

func (w *Where[T]) Parameters() []*graph.Parameter[T]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL