-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d3fe7a
commit 81fbbcc
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package erf | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/advancedclimatesystems/gonnx/onnx" | ||
"github.com/advancedclimatesystems/gonnx/ops" | ||
"gorgonia.org/tensor" | ||
) | ||
|
||
var erfTypeConstraints = [][]tensor.Dtype{ops.NumericTypes} | ||
|
||
// Erf represents the ONNX erf operator. | ||
type Erf struct { | ||
ops.BaseOperator | ||
} | ||
|
||
// newSin creates a new erf operator. | ||
func newErf(version int, typeConstraints [][]tensor.Dtype) ops.Operator { | ||
return &Erf{ | ||
BaseOperator: ops.NewBaseOperator( | ||
version, | ||
1, | ||
1, | ||
typeConstraints, | ||
"erf", | ||
), | ||
} | ||
} | ||
|
||
// Init initializes the erf operator. | ||
func (e *Erf) Init(*onnx.NodeProto) error { | ||
return nil | ||
} | ||
|
||
// Apply applies the erf operator. | ||
func (e *Erf) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) { | ||
var ( | ||
out tensor.Tensor | ||
err error | ||
) | ||
|
||
switch inputs[0].Dtype() { | ||
case tensor.Uint8: | ||
out, err = inputs[0].Apply(erf[uint8]) | ||
case tensor.Uint16: | ||
out, err = inputs[0].Apply(erf[uint16]) | ||
case tensor.Uint32: | ||
out, err = inputs[0].Apply(erf[uint32]) | ||
case tensor.Uint64: | ||
out, err = inputs[0].Apply(erf[uint64]) | ||
case tensor.Int8: | ||
out, err = inputs[0].Apply(erf[int8]) | ||
case tensor.Int16: | ||
out, err = inputs[0].Apply(erf[int16]) | ||
case tensor.Int32: | ||
out, err = inputs[0].Apply(erf[int32]) | ||
case tensor.Int64: | ||
out, err = inputs[0].Apply(erf[int64]) | ||
case tensor.Float32: | ||
out, err = inputs[0].Apply(erf[float32]) | ||
case tensor.Float64: | ||
out, err = inputs[0].Apply(erf[float64]) | ||
default: | ||
return nil, ops.ErrInvalidInputType(0, inputs[0].Dtype().String(), e.BaseOperator) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []tensor.Tensor{out}, nil | ||
} | ||
|
||
func erf[T ops.NumericType](x T) T { | ||
return T(math.Erf(float64(x))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package erf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/advancedclimatesystems/gonnx/ops" | ||
"github.com/stretchr/testify/assert" | ||
"gorgonia.org/tensor" | ||
) | ||
|
||
func TestErfInit(t *testing.T) { | ||
e := &Erf{} | ||
err := e.Init(nil) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestErf(t *testing.T) { | ||
tests := []struct { | ||
version int64 | ||
backing []float32 | ||
shape []int | ||
expected []float32 | ||
}{ | ||
{ | ||
9, | ||
[]float32{-1, -1, 0, 1}, | ||
[]int{2, 2}, | ||
[]float32{-0.8427008, -0.8427008, 0, 0.8427008}, | ||
}, | ||
{ | ||
13, | ||
[]float32{1, 0.5, 0.0, -0.5}, | ||
[]int{1, 4}, | ||
[]float32{0.8427008, 0.5204999, 0, -0.5204999}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
inputs := []tensor.Tensor{ | ||
ops.TensorWithBackingFixture(test.backing, test.shape...), | ||
} | ||
|
||
erf := erfVersions[test.version]() | ||
|
||
res, err := erf.Apply(inputs) | ||
assert.Nil(t, err) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, test.expected, res[0].Data()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package erf | ||
|
||
import ( | ||
"github.com/advancedclimatesystems/gonnx/ops" | ||
) | ||
|
||
var erfVersions = ops.OperatorVersions{ | ||
9: ops.NewOperatorConstructor(newErf, 9, erfTypeConstraints), | ||
13: ops.NewOperatorConstructor(newErf, 13, erfTypeConstraints), | ||
} | ||
|
||
func GetVersions() ops.OperatorVersions { | ||
return erfVersions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters