gotch

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2020 License: Apache-2.0 Imports: 4 Imported by: 50

README

GoTch LicenseGo.Dev referenceTravis CIGo Report Card

Overview

  • GoTch is a C++ Libtorch Go binding for developing and implementing deep learning projects in Go.
  • This package is to create a thin wrapper of Libtorch to make use of its tensor APIs and CUDA support while implementing as much idiomatic Go as possible.
  • There are about 1129 auto-generated tensor APIs.

Dependencies

  • Libtorch C++ v1.5.0 library of Pytorch

Installation

  • CPU

    Default values: LIBTORCH_VER=1.5.1 and GOTCH_VER=v0.1.7

    go get -u github.com/sugarme/gotch@v0.1.7
    bash ${GOPATH}/pkg/mod/github.com/sugarme/gotch@v0.1.7/setup-cpu.sh
    
    
  • GPU

    Default values: LIBTORCH_VER=1.5.1, CUDA_VER=10.1 and GOTCH_VER=v0.1.7

    go get -u github.com/sugarme/gotch@v0.1.7
    bash ${GOPATH}/pkg/mod/github.com/sugarme/gotch@v0.1.7/setup-gpu.sh
    
    

Examples

Basic tensor operations

import (
	"fmt"

	"github.com/sugarme/gotch"
	ts "github.com/sugarme/gotch/tensor"
)

func basicOps() {

	// Initiate a tensor
	tensor := ts.MustArange1(ts.FloatScalar(0), ts.FloatScalar(12), gotch.Float, gotch.CPU).MustView([]int64{3, 4}, true)

	tensor.Print()
    //  0   1   2   3
    //  4   5   6   7
    //  8   9  10  11
    // [ CPUFloatType{3,4} ]

	fmt.Printf("tensor values: %v\n", tensor.Float64Values())
    //tensor values: [0 1 2 3 4 5 6 7 8 9 10 11]

	fmt.Printf("tensor dtype: %v\n", tensor.DType())
    //tensor dtype: float32

	fmt.Printf("tensor shape: %v\n", tensor.MustSize())
    //tensor shape: [3 4]

	fmt.Printf("tensor element number: %v\n", tensor.Numel())
    //tensor element number: 12

	// Delete a tensor (NOTE. tensor is created in C memory and will need to free up manually.)
	tensor.MustDrop()

	// Basic tensor operations
	ts1 := ts.MustArange(ts.IntScalar(6), gotch.Int64, gotch.CPU).MustView([]int64{2, 3}, true)
	defer ts1.MustDrop()
	ts2 := ts.MustOnes([]int64{3, 4}, gotch.Int64, gotch.CPU)
	defer ts2.MustDrop()

	mul := ts1.MustMatmul(ts2, false)
	defer mul.MustDrop()
	fmt.Println("ts1: ")
	ts1.Print()
	fmt.Println("ts2: ")
	ts2.Print()
	fmt.Println("mul tensor (ts1 x ts2): ")
	mul.Print()

    //ts1: 
    // 0  1  2
    // 3  4  5
    //[ CPULongType{2,3} ]
    //ts2: 
    // 1  1  1  1
    // 1  1  1  1
    // 1  1  1  1
    //[ CPULongType{3,4} ]
    //mul tensor (ts1 x ts2): 
    //  3   3   3   3
    // 12  12  12  12
    //[ CPULongType{2,4} ]


	// In-place operation
	ts3 := ts.MustOnes([]int64{2, 3}, gotch.Float, gotch.CPU)
	fmt.Println("Before:")
	ts3.Print()
	ts3.MustAdd1_(ts.FloatScalar(2.0))
	fmt.Printf("After (ts3 + 2.0): \n")
	ts3.Print()
	ts3.MustDrop()

    //Before:
    // 1  1  1
    // 1  1  1
    //[ CPUFloatType{2,3} ]
    //After (ts3 + 2.0): 
    // 3  3  3
    // 3  3  3
    //[ CPUFloatType{2,3} ]

}

Simplified Convolutional neural network

    import (
        "github.com/sugarme/gotch"
        "github.com/sugarme/gotch/nn"
        ts "github.com/sugarme/gotch/tensor"
    )

    type Net struct {
        conv1 nn.Conv2D
        conv2 nn.Conv2D
        fc    nn.Linear
    }

    func newNet(vs nn.Path) Net {
        conv1 := nn.NewConv2D(vs, 1, 16, 2, nn.DefaultConv2DConfig())
        conv2 := nn.NewConv2D(vs, 16, 10, 2, nn.DefaultConv2DConfig())
        fc := nn.NewLinear(vs, 10, 10, nn.DefaultLinearConfig())

        return Net{
            conv1,
            conv2,
            fc,
        }
    }

    func (n Net) ForwardT(xs ts.Tensor, train bool) (retVal ts.Tensor) {
        xs = xs.MustView([]int64{-1, 1, 8, 8}, false)

        outC1 := xs.Apply(n.conv1)
        outMP1 := outC1.MaxPool2DDefault(2, true)
        defer outMP1.MustDrop()

        outC2 := outMP1.Apply(n.conv2)
        outMP2 := outC2.MaxPool2DDefault(2, true)
        outView2 := outMP2.MustView([]int64{-1, 10}, true)
        defer outView2.MustDrop()

        outFC := outView2.Apply(&n.fc)

        return outFC.MustRelu(true)

    }

    func main() {

        vs := nn.NewVarStore(gotch.CPU)
        net := newNet(vs.Root())

        xs := ts.MustOnes([]int64{8, 8}, gotch.Float, gotch.CPU)

        logits := net.ForwardT(xs, false)
        logits.Print()
    }

    // 0.0000  0.0000  0.0000  0.2477  0.2437  0.0000  0.0000  0.0000  0.0000  0.0171
    //[ CPUFloatType{1,10} ]


Getting Started

License

GoTch is Apache 2.0 licensed.

Acknowledgement

  • This project has been inspired and used many concepts from tch-rs Libtorch Rust binding.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CPU  Device = Device{Name: "CPU", Value: -1}
	CUDA Cuda   = Cuda{Name: "CUDA", Value: 0}
)

Functions

func DTypeSize

func DTypeSize(dt DType) (retVal uint, err error)

DTypeSize returns DType size in Bytes

func ElementGoType

func ElementGoType(data interface{}) (retVal reflect.Type, err error)

ElementGoType infers and returns Go type of element in given data

func IsSupportedScalar

func IsSupportedScalar(k reflect.Kind) bool

IsSupportedScalar checks whether given SCALAR type is supported TODO: check input is a scalar.

func ToGoType

func ToGoType(dtype DType) (retVal reflect.Type, err error)

ToGoType infers and returns supported equivalent Go type from given DType

func TypeOf

func TypeOf(dt DType, shape []int64) (retVal reflect.Type, err error)

TypeOf infers and returns element Go type from given tensor DType and shape

Types

type CInt

type CInt = int32

CInt is equal to C type int. Go type is int32

func DType2CInt

func DType2CInt(dt DType) (retVal CInt, err error)

type Cuda

type Cuda Device

func (Cuda) CudnnIsAvailable

func (cu Cuda) CudnnIsAvailable() bool

CudnnIsAvailable return true if cudnn support is available

func (Cuda) CudnnSetBenchmark

func (cu Cuda) CudnnSetBenchmark(b bool)

CudnnSetBenchmark sets cudnn benchmark mode

When set cudnn will try to optimize the generators during the first network runs and then use the optimized architecture in the following runs. This can result in significant performance improvements.

func (Cuda) DeviceCount

func (cu Cuda) DeviceCount() int64

DeviceCount returns the number of GPU that can be used.

func (Cuda) IsAvailable

func (cu Cuda) IsAvailable() bool

CudnnIsAvailable returns true if cuda support is available

type DType

type DType struct {
	reflect.Type
}

DType represents different kind of element that a tensor can hold. It has an embedded `reflect.Type` for type reflection.

var (
	Uint8 DType = DType{reflect.TypeOf(uint8(1))} // 0
	Int8  DType = DType{reflect.TypeOf(int8(1))}  // 1
	Int16 DType = DType{reflect.TypeOf(int16(1))} // 2
	Int   DType = DType{reflect.TypeOf(int32(1))} // 3
	Int64 DType = DType{reflect.TypeOf(int64(1))} // 4
	// Half       DType   = DType{reflect.TypeOf(GoFloat16(1))}     // 5
	Float  DType = DType{reflect.TypeOf(float32(1))} // 6
	Double DType = DType{reflect.TypeOf(float64(1))} // 7
	// ComplexHalf DType  = DType{reflect.TypeOf(GoComplexHalf(1))} // 8
	// ComplexFloat DType  = DType{reflect.TypeOf(complex64(1))}  // 9
	// ComplexDouble DType = DType{reflect.TypeOf(complex128(1))} // 10
	Bool DType = DType{reflect.TypeOf(true)} // 11
)

TODO: double check these Torch DType to Go type

func CInt2DType

func CInt2DType(v CInt) (dtype DType, err error)

func DTypeFromData

func DTypeFromData(data interface{}) (retVal DType, err error)

DTypeFromData infers returns equavalent DType from given data

func DataDType

func DataDType(v interface{}, shape []int64) (retVal DType, err error)

DataDType infers and returns data type of tensor data

func ElementDType

func ElementDType(v interface{}) (retVal DType, err error)

ElementDType infers and returns its own tensor data type

func ToDType

func ToDType(typ reflect.Type) (retVal DType, err error)

ToDType infers and returns supported equivalent DType from given Go type

func (DType) CInt

func (dt DType) CInt() (retVal CInt)

type DTypeDevice

type DTypeDevice struct {
	DType  DType
	Device Device
}

type Device

type Device struct {
	Name  string
	Value int
}

func CudaBuilder

func CudaBuilder(v uint) Device

func NewCuda

func NewCuda() Device

NewCuda creates a cuda device (default) if available If will be panic if cuda is not available.

func (Device) CInt

func (d Device) CInt() CInt

func (Device) CudaIfAvailable

func (d Device) CudaIfAvailable() Device

CudaIfAvailable returns a GPU device if available, else default to CPU

func (Device) IsCuda

func (d Device) IsCuda() bool

IsCuda returns whether device is a Cuda device

func (Device) OfCInt

func (d Device) OfCInt(v CInt) Device

Directories

Path Synopsis
example
jit
yolo/freetype
The freetype package provides a convenient API to draw text onto an image.
The freetype package provides a convenient API to draw text onto an image.
NOTE: functions in this file would be automatically generated and named as `c-generated.go`
NOTE: functions in this file would be automatically generated and named as `c-generated.go`

Jump to

Keyboard shortcuts

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