Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
feat: opertors
Browse files Browse the repository at this point in the history
  • Loading branch information
owulveryck committed Apr 14, 2019
1 parent 840b5e4 commit 3bb7f2b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
39 changes: 39 additions & 0 deletions backend/x/gorgonnx/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
This package provides a backend compatible with the `backend.ComputationGraph` interface.

The runtime is based on [Gorgonia](gorgonia.org/gorgonia)

## How to add new operators

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

[embedmd]:# (operator.go /type operator/ /}/)
```go
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:

[embedmd]:# (apigen_operators.go /type .* struct/ /}/)
```go
type hadamardProd struct{}
```

[embedmd]:# (apigen_operators.go /func init/ /^}/)
```go
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
28 changes: 28 additions & 0 deletions backend/x/gorgonnx/apigen_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (a *hadamardProd) apply(g *Graph, n *Node) error {
return err
}

func (a *hadamardProd) init(o onnx.Operation) error {
return nil
}


type hadamardDiv struct{}

Expand Down Expand Up @@ -62,6 +66,10 @@ func (a *hadamardDiv) apply(g *Graph, n *Node) error {
return err
}

func (a *hadamardDiv) init(o onnx.Operation) error {
return nil
}


type sub struct{}

Expand Down Expand Up @@ -91,6 +99,10 @@ func (a *sub) apply(g *Graph, n *Node) error {
return err
}

func (a *sub) init(o onnx.Operation) error {
return nil
}


type add struct{}

Expand Down Expand Up @@ -120,6 +132,10 @@ func (a *add) apply(g *Graph, n *Node) error {
return err
}

func (a *add) init(o onnx.Operation) error {
return nil
}


type cos struct{}

Expand All @@ -140,6 +156,10 @@ func (a *cos) apply(g *Graph, n *Node) error {
return err
}

func (a *cos) init(o onnx.Operation) error {
return nil
}


type sin struct{}

Expand All @@ -160,6 +180,10 @@ func (a *sin) apply(g *Graph, n *Node) error {
return err
}

func (a *sin) init(o onnx.Operation) error {
return nil
}


type tanh struct{}

Expand All @@ -180,3 +204,7 @@ func (a *tanh) apply(g *Graph, n *Node) error {
return err
}

func (a *tanh) init(o onnx.Operation) error {
return nil
}

4 changes: 4 additions & 0 deletions backend/x/gorgonnx/genapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (a *{{ .GorgonnxOp }}) apply(g *Graph, n *Node) error {
)
return err
}
func (a *{{ .GorgonnxOp }}) init(o onnx.Operation) error {
return nil
}
`

var iterator = template.FuncMap{
Expand Down
12 changes: 9 additions & 3 deletions backend/x/gorgonnx/operator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gorgonnx

import "fmt"
import (
"fmt"

"github.com/owulveryck/onnx-go"
)

func init() {
//operators = make(map[string]operator, 0)
Expand All @@ -14,9 +18,11 @@ var operators = map[string]operator{}

type operator interface {
// apply analyse the graph to find the children of the node
// the extract its gorgonia.Node references and assign the result of the operation
// to the node n
// 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
}

// check conditions of the children.
Expand Down

0 comments on commit 3bb7f2b

Please sign in to comment.