gorgonia.org/gorgonia@v0.9.17/vm_genera_cuda_test.go (about)

     1  // +build cuda
     2  
     3  package gorgonia
     4  
     5  import (
     6  	"log"
     7  	"testing"
     8  
     9  	"gorgonia.org/tensor"
    10  )
    11  
    12  func TestGeneraCUDA_init(t *testing.T) {
    13  	g, x, y, z := simpleMatEqn()
    14  	zs := Must(Slice(z, S(0))) // not a CUDA op (for now)
    15  	ex := NewVector(g, Float64, WithName("extra"), WithShape(2))
    16  	xs := Must(Slice(x, S(1)))
    17  	zpe := Must(Add(zs, ex))
    18  	zpepxpe := Must(Add(xs, zpe))
    19  	Must(Sum(zpepxpe))
    20  
    21  	xV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{0, 1, 2, 3}))
    22  	yV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{5, 4, 3, 2}))
    23  	eV := tensor.New(tensor.WithShape(2), tensor.WithBacking([]float64{1000, 50000}))
    24  
    25  	Let(x, xV)
    26  	Let(y, yV)
    27  	Let(ex, eV)
    28  
    29  	// logger := log.New(os.Stderr, "", 0)
    30  	// m := NewLispMachine(g, WithLogger(logger), WithWatchlist(), LogBothDir())
    31  	m := NewLispMachine(g)
    32  	defer m.Close()
    33  
    34  	t.Logf("%v", m.sorted)
    35  	t.Logf("%v %v", m.cpumem, m.gpumem)
    36  	t.Logf("%v", m.df.devTransChildren)
    37  	t.Logf("%v", m.df.devTransRepl[m.sorted[0]])
    38  	if err := m.RunAll(); err != nil {
    39  		t.Errorf("Error %v", err)
    40  	}
    41  
    42  	for _, n := range m.sorted {
    43  		if n.boundTo != nil {
    44  			if dv, ok := n.boundTo.(*dualValue); ok {
    45  				log.Printf("\tEncountered %v 0x%x | 0x%x", n, dv.Value.Uintptr(), dv.d.Uintptr())
    46  			} else {
    47  				log.Printf("\tEncountered %v 0x%x", n, n.boundTo.Uintptr())
    48  			}
    49  		}
    50  	}
    51  
    52  	var xG, yG Value
    53  	var err error
    54  	if xG, err = x.Grad(); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if yG, err = y.Grad(); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	t.Logf("xG:\n%v", xG)
    61  	t.Logf("yG:\n%v", yG)
    62  
    63  	// Compile(g)
    64  
    65  }
    66  
    67  func TestGenera_ForceCPU(t *testing.T) {
    68  	g, x, y, z := simpleMatEqn()
    69  
    70  	xV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{0, 1, 2, 3}))
    71  	yV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{5, 4, 3, 2}))
    72  
    73  	Let(x, xV)
    74  	Let(y, yV)
    75  	m := NewLispMachine(g, WithManualGradient())
    76  	defer m.Close()
    77  	m.ForceCPU()
    78  
    79  	if err := m.RunAll(); err != nil {
    80  		t.Errorf("%v", err)
    81  	}
    82  
    83  	t.Logf("%v", z.Value())
    84  }
    85  
    86  func TestGenera_Backprop(t *testing.T) {
    87  	g, x, y, z := simpleMatEqn()
    88  	Must(Sum(z))
    89  
    90  	xV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{0, 1, 2, 3}))
    91  	yV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{5, 4, 3, 2}))
    92  
    93  	Let(x, xV)
    94  	Let(y, yV)
    95  	m := NewLispMachine(g)
    96  
    97  	if err := m.RunAll(); err != nil {
    98  		t.Errorf("%v", err)
    99  	}
   100  	t.Logf("x.Value: 0x%x | d: 0x%x", x.boundTo.(*dualValue).Value.Uintptr(), x.boundTo.(*dualValue).d.Uintptr())
   101  }