gorgonnx

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2019 License: MIT Imports: 20 Imported by: 12

README

This package provides a backend compatible with the backend.ComputationGraph interface.

The runtime is based on Gorgonia

How to add new operators

An operator is basically an object that must fulfills the interface:

type operator interface {
	// apply analyse the graph to find the children of the node
	// then extract its gorgonia.Node references
	// and assign the result of the operation to the node n
	apply(g *Graph, n *Node) error
	// init the operator with name and attributes as carried by the onnx.Operator
	init(o onnx.Operation) error
}

The operator must be registered to be usable, this is typically done within an init function:

type hadamardProd struct{}
func init() {
	register("Mul", &hadamardProd{})
}

Tests

All the registered operators are tested against the official onnx tests if they exists. simply run go test -v to check it out

ApiGen

For common arithmetic operators, a genapi command can be found in a subdirectory of this package

Documentation

Overview

Package gorgonnx creates a temporary graph that is compatible with backend.ComputationBackend.

Example (FizzBuzz)
package main

import (
	"fmt"
	"log"

	"github.com/owulveryck/onnx-go"
	"gorgonia.org/tensor"
)

func bin(n int, numDigits int) []float32 {
	f := make([]float32, numDigits)
	for i := uint(0); i < uint(numDigits); i++ {
		f[i] = float32((n >> i) & 1)
	}
	return f[:]
}

func dec(b []float32) int {
	for i := 0; i < len(b); i++ {
		if b[i] > 0.4 {
			return i
		}
	}
	panic("Sorry, I'm wrong")
}

func display(v []float32, i int) {
	switch dec(v) {
	case 0:
		fmt.Println(i)
	case 1:
		fmt.Println("Fizz")
	case 2:
		fmt.Println("Buzz")
	case 3:
		fmt.Println("FizzBuzz")
	}
}

func main() {
	backend := NewGraph()

	m := onnx.NewModel(backend)

	err := m.UnmarshalBinary(fizzBuzzOnnx)

	if err != nil {
		log.Fatal(err)
	}

	input := tensor.New(tensor.WithShape(7), tensor.Of(tensor.Float32))
	for i := 1; i <= 100; i++ {
		for j, v := range bin(i, 7) {
			input.SetAt(v, j)
		}
		m.SetInput(0, input)
		err = backend.Run()
		if err != nil {
			log.Fatal(err)
		}
		output, err := m.GetOutputTensors()
		if err != nil {
			log.Fatal(err)
		}
		display(output[0].Data().([]float32), i)
	}
}
Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleOperator

func ExampleOperator()

ExampleOperator implementation of an operator

Types

type Graph

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

Graph is the top structure that should be compatible with

backend.ComputationGraph

It holds a gorgonia.ExprGraph that is populated on the first call to the Run() method

func NewGraph

func NewGraph() *Graph

NewGraph ...

func (*Graph) AddNode

func (g *Graph) AddNode(n graph.Node)

AddNode ...

func (*Graph) ApplyOperation

func (g *Graph) ApplyOperation(o onnx.Operation, ns ...graph.Node) error

ApplyOperation to fulfill the onnx.Backend interface

func (*Graph) Edge

func (g *Graph) Edge(uid, vid int64) graph.Edge

Edge ...

func (*Graph) From

func (g *Graph) From(id int64) graph.Nodes

From ...

func (*Graph) GetExprGraph

func (g *Graph) GetExprGraph() (*gorgonia.ExprGraph, error)

GetExprGraph returns the gorgonia graph; if the graph is nil, it populates the graph before returing it

func (*Graph) HasEdgeBetween

func (g *Graph) HasEdgeBetween(xid, yid int64) bool

HasEdgeBetween ...

func (*Graph) HasEdgeFromTo

func (g *Graph) HasEdgeFromTo(uid, vid int64) bool

HasEdgeFromTo ...

func (*Graph) NewNode

func (g *Graph) NewNode() graph.Node

NewNode ...

func (*Graph) NewWeightedEdge

func (g *Graph) NewWeightedEdge(from, to graph.Node, w float64) graph.WeightedEdge

NewWeightedEdge returns a new WeightedEdge from the source to the destination node. Fulfills the graph.WeightedEdgeAdder interface

func (*Graph) Node

func (g *Graph) Node(id int64) graph.Node

Node ...

func (*Graph) Nodes

func (g *Graph) Nodes() graph.Nodes

Nodes ...

func (*Graph) PopulateExprgraph

func (g *Graph) PopulateExprgraph() error

PopulateExprgraph creates the underlynig graph by walking the current graph

func (*Graph) Run

func (g *Graph) Run() error

Run the graph. It populate the underlying exprgraph if the graph is nil

func (*Graph) SetVM

func (g *Graph) SetVM(vm gorgonia.VM)

SetVM used by the backend A call to this method do not call the PopulateExprgraph method it is the responsibility of the caller to call it before

func (*Graph) SetWeightedEdge

func (g *Graph) SetWeightedEdge(e graph.WeightedEdge)

SetWeightedEdge adds an edge from one node to another. If the graph supports node addition the nodes will be added if they do not exist, otherwise SetWeightedEdge will panic. The behavior of a WeightedEdgeAdder when the IDs returned by e.From() and e.To() are equal is implementation-dependent. Whether e, e.From() and e.To() are stored. Fulfills the graph.WeightedEdgeAdder interface

func (*Graph) To

func (g *Graph) To(id int64) graph.Nodes

To ...

type Node

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

Node is compatible with graph.Node and onnx.DataCarrier

func (*Node) GetName

func (n *Node) GetName() string

GetName get the name of the node

func (*Node) GetTensor

func (n *Node) GetTensor() tensor.Tensor

GetTensor value from the node

func (*Node) ID

func (n *Node) ID() int64

ID to fulfill the graph.Node interface

func (*Node) SetName

func (n *Node) SetName(name string)

SetName set the name of the node

func (*Node) SetTensor

func (n *Node) SetTensor(t tensor.Tensor) error

SetTensor assign the tensor N to the underlying node

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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