modeldsl

package
v1.19.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Experimental — this package is not yet wired into the main framework.

Package modeldsl provides a declarative DSL for defining custom model architectures using Go structs. Model definitions are validated and compiled into runnable graphs that support forward inference.

Package modeldsl provides a declarative DSL for defining custom model architectures using Go structs. Model definitions are validated and compiled into runnable graphs that support forward inference.

Package modeldsl provides training support for DSL-defined models.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionDef

type ConnectionDef struct {
	From string
	To   string
}

ConnectionDef specifies a directed edge from one layer to another.

type LayerDef

type LayerDef struct {
	Name   string
	Type   LayerType
	Params map[string]any
}

LayerDef defines a single layer in the model.

type LayerType

type LayerType string

LayerType identifies the kind of neural network layer.

const (
	LayerLinear    LayerType = "linear"
	LayerRMSNorm   LayerType = "rmsnorm"
	LayerSiLU      LayerType = "silu"
	LayerSoftmax   LayerType = "softmax"
	LayerAttention LayerType = "attention"
)
const FusedLayerType LayerType = "fused"

FusedLayerType is the type used for fused operator layers.

type Model

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

Model is a runnable model built from a ModelGraph.

func (*Model) Forward

func (m *Model) Forward(input []float64) ([]float64, error)

Forward runs inference through the model.

func (*Model) Parameters

func (m *Model) Parameters() []*Param

Parameters returns all trainable parameters of the model.

func (*Model) Train

func (m *Model) Train(config TrainConfig, samples []Sample) (*TrainResult, error)

Train runs a training loop over the given samples using mean squared error loss. It performs gradient descent using the specified learning rate.

type ModelDef

type ModelDef struct {
	Name        string
	Layers      []LayerDef
	Connections []ConnectionDef
}

ModelDef is the top-level model definition.

type ModelGraph

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

ModelGraph is the validated internal representation of a model definition. Use Build to instantiate a runnable Model from it.

func ConstantFolding

func ConstantFolding(g *ModelGraph) *ModelGraph

ConstantFolding removes layers whose output can be determined statically. In this DSL, identity-like element-wise layers (RMSNorm, SiLU, Softmax) that feed into another element-wise layer of the same type are collapsed: the duplicate is removed because applying the same normalization/activation twice is redundant. Additionally, consecutive identical element-wise layers are folded into one.

func DeadNodeElimination

func DeadNodeElimination(g *ModelGraph) *ModelGraph

DeadNodeElimination removes layers that do not contribute to the model's terminal output. A node is "dead" if no path from it reaches any node that is both an output (no children) and reachable from an input (no parents). When there are dangling branches (leaf nodes only reachable through a fork), they are pruned because they don't contribute to the primary output path.

Specifically, it traces backward from each output that lies on a path starting from an input, matching Model.Forward semantics.

func OperatorFusion

func OperatorFusion(g *ModelGraph) *ModelGraph

OperatorFusion merges sequences of compatible layers into fused operators. Currently supported fusions:

  • RMSNorm followed by SiLU → fused "rmsnorm_silu"
  • SiLU followed by RMSNorm → fused "silu_rmsnorm"

func Optimize

func Optimize(g *ModelGraph, passes ...OptimizePass) *ModelGraph

Optimize applies a sequence of optimization passes to a graph, returning a new optimized graph. The original graph is not modified.

func Parse

func Parse(def *ModelDef) (*ModelGraph, error)

Parse validates a ModelDef and builds a ModelGraph.

func (*ModelGraph) Build

func (g *ModelGraph) Build(inputDim, outputDim int) (*Model, error)

Build instantiates a runnable Model from the graph. inputDim and outputDim specify the dimensions of the model's input and output vectors.

func (*ModelGraph) BuildTrainable

func (g *ModelGraph) BuildTrainable(inputDim, outputDim int) (*Model, error)

BuildTrainable instantiates a trainable Model from the graph. Unlike Build, the returned Model supports backward pass and parameter updates.

func (*ModelGraph) Inputs

func (g *ModelGraph) Inputs() []string

Inputs returns the names of input layers (layers with no parents).

func (*ModelGraph) Name

func (g *ModelGraph) Name() string

Name returns the model name.

func (*ModelGraph) Order

func (g *ModelGraph) Order() []string

Order returns the topological execution order of layer names.

func (*ModelGraph) Outputs

func (g *ModelGraph) Outputs() []string

Outputs returns the names of output layers (layers with no children).

type OptimizePass

type OptimizePass func(g *ModelGraph) *ModelGraph

OptimizePass is a named graph transformation.

type Param

type Param struct {
	Data []float64
	Grad []float64
}

Param holds a trainable parameter vector and its accumulated gradient.

type Sample

type Sample struct {
	Input  []float64
	Target []float64
}

Sample is a single training example.

type TrainConfig

type TrainConfig struct {
	Epochs       int
	LearningRate float64
}

TrainConfig configures a training run.

type TrainResult

type TrainResult struct {
	FinalLoss float64
	BestLoss  float64
	BestEpoch int
	EpochLoss []float64
}

TrainResult holds the outcome of training.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL