graph

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package graph builds a topologically sorted execution plan from a loaded ONNX model.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCyclicGraph indicates that the computation graph contains a
	// cyclic dependency that prevents topological sorting.
	ErrCyclicGraph = errors.New("graph: cyclic dependency detected")

	// ErrUnresolvedInput indicates that a node references an input name
	// that is not a graph input, initializer, or output of another node.
	ErrUnresolvedInput = errors.New("graph: unresolved input")

	// ErrBuildFailed indicates a general failure during graph
	// construction, such as an initializer weight conversion error.
	ErrBuildFailed = errors.New("graph: build failed")

	// ErrEmptyFusionName indicates RegisterFusion was called with an
	// empty name. Fusion names must be unique non-empty strings so
	// the registry can address them.
	ErrEmptyFusionName = errors.New("graph: empty fusion name")

	// ErrNilFusion indicates RegisterFusion was called with a nil
	// FusionFunc.
	ErrNilFusion = errors.New("graph: nil fusion function")

	// ErrFusionExists indicates RegisterFusion was called with a name
	// that is already in use.
	ErrFusionExists = errors.New("graph: fusion already registered")

	// ErrFusionNotFound indicates UnregisterFusion was called for a
	// name that is not in the registry.
	ErrFusionNotFound = errors.New("graph: fusion not found")
)

Sentinel errors returned by the graph builder.

Functions

func ApplyAllFusions

func ApplyAllFusions(g *Graph) map[string]int

ApplyAllFusions runs every registered fusion against g in registration order. It returns a map keyed by fusion name carrying the number of fusions performed by each pass; callers may use the map for logging or metrics. A nil g returns an empty map and no error.

func MustRegisterFusion

func MustRegisterFusion(name string, apply FusionFunc)

MustRegisterFusion is the panicking variant of RegisterFusion intended for init() blocks. The same caveat that applies to ops.MustRegister applies here: registration is a one-shot startup operation, so a duplicate or nil registration is always a programmer error.

func RegisterFusion

func RegisterFusion(name string, apply FusionFunc) error

RegisterFusion adds apply to the list of fusions that ApplyAllFusions invokes. It returns an error if name is empty, apply is nil, or a fusion is already registered under that name. The order in which fusions are registered determines the order in which ApplyAllFusions runs them.

func RegisteredFusions

func RegisteredFusions() []string

RegisteredFusions returns the names of every fusion currently in the registry in registration order. The returned slice is a copy and safe to mutate.

func UnregisterFusion

func UnregisterFusion(name string) error

UnregisterFusion removes the named fusion from the registry. It is primarily intended for tests that need to isolate fusion behaviour. It returns an error if name is empty or no fusion is registered under that name.

Types

type ExecutionNode

type ExecutionNode struct {
	OpType      string
	Name        string
	InputNames  []string
	OutputNames []string
	Attributes  map[string]*onnx.Attribute
}

ExecutionNode is a thin projection of an onnx.Node containing only the fields needed by the executor. Attributes is the same map pointer from the source node (no deep copy).

type FusionFunc

type FusionFunc func(g *Graph) int

FusionFunc applies a single graph rewrite pass in place and returns the number of nodes the pass fused. A return value of zero is normal and means the pattern did not match anywhere; it is not an error.

type Graph

type Graph struct {
	ExecutionOrder []*ExecutionNode
	Inputs         []InputInfo
	Outputs        []OutputInfo
	Weights        map[string]*tensor.Tensor
}

Graph is a read-only execution plan built from an ONNX model. It holds nodes in topological order, filtered runtime inputs, graph outputs, and converted initializer weights.

func Build

func Build(model *onnx.Model) (*Graph, error)

Build is the sole public entry point for constructing a computation graph from a loaded ONNX model. It converts initializer weights, performs topological sort via Kahn's algorithm, and returns a read-only execution plan.

type InputInfo

type InputInfo struct {
	Name     string
	Shape    []int
	DataType onnx.DataType
}

InputInfo describes a runtime graph input with its name, shape (converted to []int), and element data type.

type OutputInfo

type OutputInfo struct {
	Name  string
	Shape []int
}

OutputInfo describes a graph output with its name and shape (converted to []int).

Directories

Path Synopsis
fusion
concatwrite
Package concatwrite registers the (producer-Conv → Concat) tail fusion.
Package concatwrite registers the (producer-Conv → Concat) tail fusion.
conv1x1pair
Package conv1x1pair registers the 1×1 Conv pair fusion.
Package conv1x1pair registers the 1×1 Conv pair fusion.
convleakyrelu
Package convleakyrelu registers the Conv → LeakyReLU fusion.
Package convleakyrelu registers the Conv → LeakyReLU fusion.
convprelu
Package convprelu registers the Conv → PReLU fusion.
Package convprelu registers the Conv → PReLU fusion.
muladd
Package muladd registers the Mul → Add fusion.
Package muladd registers the Mul → Add fusion.
pixelshuffle
Package pixelshuffle registers the PyTorch pixel-shuffle graph rewrite (Reshape → Transpose → Reshape → single DepthToSpace).
Package pixelshuffle registers the PyTorch pixel-shuffle graph rewrite (Reshape → Transpose → Reshape → single DepthToSpace).

Jump to

Keyboard shortcuts

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