Skip to content

Commit

Permalink
Fix#111 (#112)
Browse files Browse the repository at this point in the history
* Fixed #90

* Fixed #111

* Boyscout commit to fix python/numpy testing
  • Loading branch information
chewxy authored Mar 28, 2021
1 parent e3b127e commit dd4ab8f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 18 deletions.
18 changes: 15 additions & 3 deletions defaultengine_mapreduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,27 @@ func (e StdEng) OptimizedReduce(a Tensor, axis int, firstFn, lastFn, defaultFn,
}

func (e StdEng) Sum(a Tensor, along ...int) (retVal Tensor, err error) {
return e.reduce("Sum", execution.MonotonicSum, execution.SumMethods, a, along...)
a2 := a
if v, ok := a.(View); ok && v.IsMaterializable() {
a2 = v.Materialize()
}
return e.reduce("Sum", execution.MonotonicSum, execution.SumMethods, a2, along...)
}

func (e StdEng) Min(a Tensor, along ...int) (retVal Tensor, err error) {
return e.reduce("Min", execution.MonotonicMin, execution.MinMethods, a, along...)
a2 := a
if v, ok := a.(View); ok && v.IsMaterializable() {
a2 = v.Materialize()
}
return e.reduce("Min", execution.MonotonicMin, execution.MinMethods, a2, along...)
}

func (e StdEng) Max(a Tensor, along ...int) (retVal Tensor, err error) {
return e.reduce("Max", execution.MonotonicMax, execution.MaxMethods, a, along...)
a2 := a
if v, ok := a.(View); ok && v.IsMaterializable() {
a2 = v.Materialize()
}
return e.reduce("Max", execution.MonotonicMax, execution.MaxMethods, a2, along...)
}

func (e StdEng) reduce(
Expand Down
40 changes: 25 additions & 15 deletions dense_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tensor
import (
"bytes"
"encoding/gob"
"io/ioutil"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -30,6 +31,19 @@ func TestSaveLoadNumpy(t *testing.T) {
T1D.WriteNpy(f1D)
f1D.Close()

defer func() {
// cleanup
err := os.Remove("test.npy")
if err != nil {
t.Error(err)
}

err = os.Remove("test1D.npy")
if err != nil {
t.Error(err)
}
}()

script := "import numpy as np\nx = np.load('test.npy')\nprint(x)\nx = np.load('test1D.npy')\nprint(x)"
// Configurable python command, in order to be able to use python or python3
pythonCommand := os.Getenv("PYTHON_COMMAND")
Expand All @@ -42,6 +56,10 @@ func TestSaveLoadNumpy(t *testing.T) {
if err != nil {
t.Error(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
t.Error(err)
}

go func() {
defer stdin.Close()
Expand All @@ -56,29 +74,21 @@ func TestSaveLoadNumpy(t *testing.T) {
t.Logf("Do you have a python with numpy installed? You can change the python interpreter by setting the environment variable PYTHON_COMMAND. Current value: PYTHON_COMMAND=%s", pythonCommand)
}

importError := `ImportError: No module named numpy`
slurpErr, _ := ioutil.ReadAll(stderr)
if ok, _ := regexp.Match(importError, slurpErr); ok {
t.Skipf("Skipping numpy test. It would appear that you do not have Numpy installed.")
}

if err := cmd.Wait(); err != nil {
t.Error(err)
t.Errorf("%q", err.Error())
}

expected := `\[\[\s*1\.\s*5\.\]\n \[\s*10\.\s*-1\.\]\]\n`
if ok, _ := regexp.Match(expected, buf.Bytes()); !ok {
t.Errorf("Did not successfully read numpy file, \n%q\n%q", buf.String(), expected)
}

if buf.String() != expected {
}

// cleanup
err = os.Remove("test.npy")
if err != nil {
t.Error(err)
}

err = os.Remove("test1D.npy")
if err != nil {
t.Error(err)
}

// ok now to test if it can read
T2 := new(Dense)
buf = new(bytes.Buffer)
Expand Down
14 changes: 14 additions & 0 deletions dense_reduction_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions example_dense_matop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,68 @@ func ExampleDense_Slice_viewMutation() {
//
}

func ExampleView() {
// Slicing creates a "view" on the original tensor
T := New(WithBacking(Range(Int, 0, 16)), WithShape(4, 4))
fmt.Printf("T:\n%v\n", T)
V, _ := T.Slice(makeRS(1, 3), makeRS(1, 3))
fmt.Printf("V:\n%v\n", V)

// Now we modify V's 0th value
V.(*Dense).Set(0, 1000)
fmt.Printf("V[0] = 1000:\n%v\n", V)
fmt.Printf("T is also mutated:\n%v\n", T)

// Now we materialize the views
fmt.Printf("V is Materializable: %v\n", V.IsMaterializable())
T2 := V.Materialize()
fmt.Printf("T2 == V:\n%v\n", T2)

// Once materialized, it is decoupled from the original tensor
T2.(*Dense).Set(0, 999)
fmt.Printf("T2 is mutated:\n%v\nBut T is not mutated:\n%v\nNeither is V:\n%v", T2, T, V)
// Output:
// T:
// ⎡ 0 1 2 3⎤
// ⎢ 4 5 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣12 13 14 15⎦
//
// V:
// ⎡ 5 6⎤
// ⎣ 9 10⎦
//
// V[0] = 1000:
// ⎡1000 6⎤
// ⎣ 9 10⎦
//
// T is also mutated:
// ⎡ 0 1 2 3⎤
// ⎢ 4 1000 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣ 12 13 14 15⎦
//
// V is Materializable: true
// T2 == V:
// ⎡1000 6⎤
// ⎣ 9 10⎦
//
// T2 is mutated:
// ⎡999 6⎤
// ⎣ 9 10⎦
//
// But T is not mutated:
// ⎡ 0 1 2 3⎤
// ⎢ 4 1000 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣ 12 13 14 15⎦
//
// Neither is V:
// ⎡1000 6⎤
// ⎣ 9 10⎦

}

func ExampleDense_Hstack() {
var T, T1, T2, T3 *Dense
var err error
Expand Down
43 changes: 43 additions & 0 deletions example_mapreduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ func ExampleSum() {
// Summed along (1, 0): 6
}

func ExampleSum_sliced() {
T := New(WithBacking([]float64{0, 1, 2, 3}), WithShape(2, 2))
fmt.Printf("T:\n%v\n", T)

V, _ := T.Slice(nil, sli(1))
fmt.Printf("V:\n%v\n", V)

Σ, _ := Sum(V)
fmt.Printf("Σ: %v", Σ)

// Output:
// T:
// ⎡0 1⎤
// ⎣2 3⎦
//
// V:
// [1 3]
// Σ: 4

}

func ExampleArgmax() {
T := New(WithBacking([]float64{0, 100, 200, 3}), WithShape(2, 2))
fmt.Printf("T:\n%v\n", T)
Expand All @@ -49,6 +70,28 @@ func ExampleArgmax() {
// Argmax is *tensor.Dense of int
}

func ExampleArgmax_sliced() {
T := New(WithBacking([]float64{0, 100, 200, 3}), WithShape(2, 2))
fmt.Printf("T:\n%v\n", T)

// slice creates a view
V, _ := T.Slice(nil, sli(1))

// argmax along the x-axis
am, _ := Argmax(V, 0)
fmt.Printf("Argmax: %v\n", am)
fmt.Printf("Argmax is %T of %v", am, am.Dtype())

// Output:
// T:
// ⎡ 0 100⎤
// ⎣200 3⎦
//
// Argmax: 0
// Argmax is *tensor.Dense of int

}

func ExampleArgmin() {
T := New(WithBacking([]float64{0, 100, 200, 3}), WithShape(2, 2))
fmt.Printf("T:\n%v\n", T)
Expand Down

0 comments on commit dd4ab8f

Please sign in to comment.