gotorch

package module
v0.0.0-...-9afed2f Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 10 Imported by: 10

README

GoTorch

TravisCI codecov CircleCI GoDoc

GoTorch reimplements PyTorch high-level APIs, including modules and functionals, in idiomatic Go. Thus enables deep learning programming in Go and Go+. This project is in its very early stage.

Easy Switch

Writing deep learning systems in Go is as efficiently as in Python. The DCGAN training programs in GoTorch and PyTorch call similar APIs, have similar program structure, and have a similar number of lines. Go+ has a syntax similar to Python. The Go+ compiler translates Go+ programs into Go source programs. It is a joy to write Go+ programs that calls Go packages like GoTorch.

We have a plan of a translator that migrates existing PyTorch models in Python into GoTorch.

Benefits

  1. Higher runtime efficiency. Go programs run as efficiently as C++.

  2. Training and prediction in the same language. No longer training in Python and online prediction in C++. All in Go/Go+. No TensorFlow graphs or PyTorch tracing.

  3. Same data processing code for training and prediction. No need to Wrap OpenCV functions into TensorFlow operators in C++ for prediction and Python for training.

  4. Supports many machine learning paradigms., including adversarial, reinforcement, and imitation learning -- those we cannot split into training and prediction.

  5. Same program for edge and cloud. GoTorch programs compile and run on phones and self-driving cars as they do on servers and desktops.

The Tech Stack

GoTorch works with the following open-source communities to form Go+Torch.

  • the Go+ community,
  • the PyTorch community, and
  • the TensorFlow XLA ecosystem.

The following figure reveals the stack of technologies.

Go+ applications   # users write DL applications in Go+,
     │             # whose syntax is as concise as Python
 [Go+ compiler]
     ↓
Go source code -→ GoTorch -→ libtorch -→ pytorch/xla -→ XLA ops
     │
 [Go compiler]
     ↓
executable binary  # x86_64, ARM, CUDA, TPU
                   # Linux, macOS, Android, iOS

Documentation

Documentation

Index

Examples

Constants

View Source
const (
	// Byte Dtype 0
	Byte int8 = iota
	// Char Dtype 1
	Char
	// Short Dtype 2
	Short
	// Int Dtype 3
	Int
	// Long Dtype 4
	Long
	// Half Dtype 5
	Half
	// Float Dtype 6
	Float
	// Double Dtype 7
	Double
	// ComplexHalf Dtype 8
	ComplexHalf
	// ComplexFloat Dtype 9
	ComplexFloat
	// ComplexDouble Dtype 10
	ComplexDouble
	// Bool Dtype 11
	Bool
	// QInt8 Dtype 12
	QInt8
	// QUInt8 Dtype 13
	QUInt8
	// QInt32 Dtype 14
	QInt32
	// BFloat16 Dtype 15
	BFloat16
	// Invalid Dtype
	Invalid = -1
)

Variables

This section is empty.

Functions

func AllClose

func AllClose(a, b Tensor) bool

AllClose returns true if the float tensor are all close.

func Equal

func Equal(a, b Tensor) bool

Equal compares two tensors by their content.

func FinishGC

func FinishGC()

FinishGC should be called right after a train/predict loop

func GC

func GC()

GC should be called at the beginning inside a train/predict loop

func IsCUDAAvailable

func IsCUDAAvailable() bool

IsCUDAAvailable returns true if CUDA is available

func IsCUDNNAvailable

func IsCUDNNAvailable() bool

IsCUDNNAvailable returns true if cuDNN is available

func MustNil

func MustNil(err unsafe.Pointer)

MustNil asserts error to be nil

func SetCurrentCUDAStream

func SetCurrentCUDAStream(stream CUDAStream)

SetCurrentCUDAStream set stream as the current CUDA stream

func SetNumThreads

func SetNumThreads(n int32)

SetNumThreads sets the number of threads created by GoTorch

func SetTensorFinalizer

func SetTensorFinalizer(t *unsafe.Pointer)

SetTensorFinalizer sets a finalizer to the tensor

func TopK

func TopK(a Tensor, k, dim int64, largest, sorted bool) (Tensor, Tensor)

TopK torch.topk

Types

type CUDAStream

type CUDAStream struct {
	P C.CUDAStream
}

CUDAStream struct wrapped Nvidia CUDA Stream

func GetCurrentCUDAStream

func GetCurrentCUDAStream(device Device) CUDAStream

GetCurrentCUDAStream returns the current stream on device

func NewCUDAStream

func NewCUDAStream(device Device) CUDAStream

NewCUDAStream returns a new CUDA stream from the pool

func (CUDAStream) Query

func (s CUDAStream) Query() bool

Query returns true if all tasks completed on this CUDA stream

func (CUDAStream) Synchronize

func (s CUDAStream) Synchronize()

Synchronize wait until all tasks completed on this CUDA stream

type Device

type Device struct {
	T C.Device
}

Device wrapper a pointer to C.Device

func NewDevice

func NewDevice(deviceType string) Device

NewDevice returns a Device

type Optimizer

type Optimizer struct {
	Opt *C.Optimizer
}

Optimizer struct

func Adam

func Adam(lr, beta1, beta2, weightDecay float64) Optimizer

Adam creates an Adam Optimizer

func SGD

func SGD(lr, momentum, dampening, weightDecay float64, nesterov bool) Optimizer

SGD creates a SGD Optimizer

Example
package main

import (
	"fmt"

	torch "github.com/wangkuiyi/gotorch"
	nn "github.com/wangkuiyi/gotorch/nn"
)

type myNet struct {
	nn.Module
	L1, L2 *nn.LinearModule
}

// MyNet returns a MyNet instance
func newMyNet() *myNet {
	n := &myNet{
		L1: nn.Linear(100, 200, false),
		L2: nn.Linear(200, 10, false),
	}
	n.Init(n)
	return n
}

// Forward executes the calculation
func (n *myNet) Forward(x torch.Tensor) torch.Tensor {
	x = n.L1.Forward(x)
	x = n.L2.Forward(x)
	return x
}

func main() {
	net := newMyNet()
	np := net.NamedParameters()
	fmt.Println(len(np))

	opt := torch.SGD(0.1, 0, 0, 0, false)
	opt.AddParameters(net.Parameters())

	for i := 0; i < 100; i++ {
		torch.GC()
		data := torch.RandN([]int64{32, 100}, false)
		pre := net.Forward(data)
		loss := torch.Sum(pre)

		opt.ZeroGrad()
		loss.Backward()
		opt.Step()
	}
	torch.FinishGC()
	opt.Close()
}
Output:

2

func (Optimizer) AddParameters

func (opt Optimizer) AddParameters(tensors []Tensor)

AddParameters adds parameters

func (Optimizer) Close

func (opt Optimizer) Close()

Close the optimizer

func (Optimizer) SetLR

func (opt Optimizer) SetLR(lr float64)

SetLR sets learning rate

func (Optimizer) Step

func (opt Optimizer) Step()

Step updates parameters

func (Optimizer) ZeroGrad

func (opt Optimizer) ZeroGrad()

ZeroGrad reset gradients to zero

type Tensor

type Tensor struct {
	T *unsafe.Pointer
}

Tensor wrappers a pointer to C.Tensor

func Add

func Add(a, other Tensor, alpha float32) Tensor

Add torch.add

func Arange

func Arange(begin, end, step float32, requiresGrad bool) Tensor

Arange returns a 1-D tensor in range [begin, end) with common difference step beginning from begin

func Div

func Div(a, other Tensor) Tensor

Div torch.div

func Empty

func Empty(shape []int64, requiresGrad bool) Tensor

Empty returns a tensor filled with random number, torch.empty

func Eq

func Eq(a, other Tensor) Tensor

Eq wraps torch.eq, which does element-wise comparison between two tensors and returns a tensor of the same size as the operands.

func ExpandAs

func ExpandAs(a, other Tensor) Tensor

ExpandAs torch.expand_as

func Eye

func Eye(n, m int64, requiresGrad bool) Tensor

Eye returns a tensor with 1s on diagonal and 0s elsewhere

func Flatten

func Flatten(a Tensor, startDim, endDim int64) Tensor

Flatten torch.flatten

func FromBlob

func FromBlob(data unsafe.Pointer, dtype int8, sizes []int64) Tensor

FromBlob returns a deep copy Tensor with the given data memory

func Full

func Full(shape []int64, value float32, requiresGrad bool) Tensor

Full returns a tensor with all elements being given `value`

func IndexSelect

func IndexSelect(a Tensor, dim int64, index Tensor) Tensor

IndexSelect torch.index_select

func LeakyRelu

func LeakyRelu(t Tensor, negativeSlope float64) Tensor

LeakyRelu returns leaky relu of the tensor according to negativeSlope

func Linspace

func Linspace(begin, end float32, steps int64, requiresGrad bool) Tensor

Linspace returns a 1-D Tensor in range [begin, end] with steps points

func Load

func Load(path string) Tensor

Load tensor from a file

func LogSoftmax

func LogSoftmax(t Tensor, dim int64) Tensor

LogSoftmax returns log softmax of the input tensor

func Logspace

func Logspace(begin, end float32, steps int64,
	base float64, requiresGrad bool) Tensor

Logspace returns a 1-D Tensor of steps points logarithmically spaced with base base between pow(base, begin) and pow(base, end)

func MM

func MM(a, b Tensor) Tensor

MM multiplies each element of the input two tensors

func Mean

func Mean(t Tensor) Tensor

Mean returns mean of the current tensor

func Mul

func Mul(a, other Tensor) Tensor

Mul torch.mul

func NewTensor

func NewTensor(data interface{}, options ...map[string]interface{}) Tensor

NewTensor creates a tensor from a Go slice. We use variadic parameters of type map[string]interface{} to mimic named variadic parameters.

func Ones

func Ones(shape []int64, requiresGrad bool) Tensor

Ones return a tensor filled with 1

func Rand

func Rand(shape []int64, requireGrad bool) Tensor

Rand torch.rand

func RandN

func RandN(shape []int64, requiresGrad bool) Tensor

RandN returns a tensor filled with standard normal distribution, torch.randn

func Relu

func Relu(t Tensor) Tensor

Relu returns relu of the tensor

func Sigmoid

func Sigmoid(t Tensor) Tensor

Sigmoid returns sigmoid of the current tensor

func Squeeze

func Squeeze(t Tensor, dim ...int64) Tensor

Squeeze torch.squeeze

func Stack

func Stack(tensors []Tensor, dim int64) Tensor

Stack concatenates sequence of tensors along a new dimension

func Sub

func Sub(a, other Tensor, alpha float32) Tensor

Sub torch.sub

func Sum

func Sum(a Tensor, opt ...map[string]interface{}) Tensor

Sum is torch.sum

func Tanh

func Tanh(t Tensor) Tensor

Tanh returns tanh of the current tensor

func To

func To(a Tensor, device Device, dtype int8) Tensor

To returns a Tensor on the specified device with the same content as the a. If the specified device doesn't exist, To panics.

func Transpose

func Transpose(a Tensor, dim0, dim1 int64) Tensor

Transpose torch.transpose

func View

func View(a Tensor, shape ...int64) Tensor

View returns a new Tensor with the same data but of a different shape

func (*Tensor) Add

func (a *Tensor) Add(other Tensor, alpha float32) Tensor

Add torch.add

func (*Tensor) AddI

func (a *Tensor) AddI(other Tensor, alpha float32) Tensor

AddI adds in-place

func (Tensor) Argmax

func (a Tensor) Argmax(opts ...interface{}) Tensor

Argmax mimics torch.argmax

func (Tensor) Argmin

func (a Tensor) Argmin(opts ...interface{}) Tensor

Argmin mimics torch.argmin

func (Tensor) Backward

func (a Tensor) Backward()

Backward compute the gradient of current tensor

func (Tensor) CUDA

func (a Tensor) CUDA(device Device, nonBlocking bool) Tensor

CUDA returns a Tensor on CUDA device

func (Tensor) CastTo

func (a Tensor) CastTo(dtype int8) Tensor

CastTo cast tensor dtype

func (Tensor) CopyTo

func (a Tensor) CopyTo(device Device) Tensor

CopyTo cast tensor dtype

func (*Tensor) Detach

func (a *Tensor) Detach() Tensor

Detach tensor.detach

func (Tensor) Dim

func (a Tensor) Dim() int64

Dim returns dim

func (*Tensor) Div

func (a *Tensor) Div(other Tensor) Tensor

Div torch.Div

func (*Tensor) DivI

func (a *Tensor) DivI(other Tensor) Tensor

DivI run divides in-place

func (Tensor) Dtype

func (a Tensor) Dtype() int8

Dtype returns data type

func (Tensor) Eq

func (a Tensor) Eq(other Tensor) Tensor

Eq torch.eq

func (Tensor) ExpandAs

func (a Tensor) ExpandAs(other Tensor) Tensor

ExpandAs torch.expand_as

func (*Tensor) GobDecode

func (t *Tensor) GobDecode(buf []byte) error

GobDecode makes Tensor implements the gob.GobDecoder interface.

func (Tensor) GobEncode

func (t Tensor) GobEncode() ([]byte, error)

GobEncode calls C.Tensor_Encode to encode a tensor into a pickle. If the tensor is on GPU, C.Tenosr_Encode clones it in CPU before encoding, so the result always encodes a CPU tensor.

func (Tensor) Grad

func (a Tensor) Grad() Tensor

Grad returns a reference of the gradient

func (Tensor) Index

func (a Tensor) Index(index ...int64) Tensor

Index calls Tensor::index to return a single-element tensor of the element at the given index.

func (Tensor) IndexSelect

func (a Tensor) IndexSelect(dim int64, index Tensor) Tensor

IndexSelect torch.index_select

func (Tensor) Item

func (a Tensor) Item() interface{}

Item returns 0-dim tensor's value as an interface users should do type assertion and get the value like: v, ok := a.Item().(float64) Currently not support unsigned Tensor.

func (*Tensor) LeakyRelu

func (a *Tensor) LeakyRelu(negativeSlope float64) Tensor

LeakyRelu returns leaky relu of the tensor according to negativeSlope

func (Tensor) LogSoftmax

func (a Tensor) LogSoftmax(dim int64) Tensor

LogSoftmax returns log softmax of the current tensor

func (Tensor) Mean

func (a Tensor) Mean() Tensor

Mean torch.mean

func (*Tensor) Mul

func (a *Tensor) Mul(other Tensor) Tensor

Mul torch.Mul

func (*Tensor) MulI

func (a *Tensor) MulI(other Tensor) Tensor

MulI multiplies in-place

func (*Tensor) Permute

func (a *Tensor) Permute(dims []int64) Tensor

Permute transpose the tensor dims.

func (Tensor) PinMemory

func (a Tensor) PinMemory() Tensor

PinMemory returns a tensor in pinned memory. Pinned memory requires CUDA.

func (Tensor) Print

func (a Tensor) Print()

Print the tensor

func (*Tensor) Relu

func (a *Tensor) Relu() Tensor

Relu returns relu of the tensor

func (Tensor) Save

func (a Tensor) Save(path string)

Save the tensor to a file

func (Tensor) SetData

func (a Tensor) SetData(b Tensor)

SetData sets the tensor data held by b to a

func (Tensor) Shape

func (a Tensor) Shape() []int64

Shape returns shape

func (Tensor) Sigmoid

func (a Tensor) Sigmoid() Tensor

Sigmoid returns sigmoid of the current tensor

func (Tensor) Squeeze

func (a Tensor) Squeeze(dim ...int64) Tensor

Squeeze tensor.squeeze

func (Tensor) String

func (a Tensor) String() string

String returns the Tensor as a string

func (*Tensor) Sub

func (a *Tensor) Sub(other Tensor, alpha float32) Tensor

Sub torch.sub

func (*Tensor) SubI

func (a *Tensor) SubI(other Tensor, alpha float32) Tensor

SubI subs in-place

func (Tensor) Sum

func (a Tensor) Sum(opt ...map[string]interface{}) Tensor

Sum is Tensor.sum

func (Tensor) Tanh

func (a Tensor) Tanh() Tensor

Tanh returns tanh of the current tensor

func (Tensor) To

func (a Tensor) To(device Device, dtype ...int8) Tensor

To returns a Tensor on the specified device with the same content as the a. If the specified device doesn't exist, To panics.

func (Tensor) Transpose

func (a Tensor) Transpose(dim0, dim1 int64) Tensor

Transpose torch.transpose

func (Tensor) View

func (a Tensor) View(shape ...int64) Tensor

View returns a new Tensor with the same data but of a different shape

Directories

Path Synopsis
cmd
example
nn
tool
tarball_divide
tarball_divide takes a .tar.gz file of images and outputs one or more .tar.gz of images, where each contains images in a certain base directory.
tarball_divide takes a .tar.gz file of images and outputs one or more .tar.gz of images, where each contains images in a certain base directory.
tarball_merge
This program is supposed to run together with tarball_divide, which divides a .tar.gz file into one or more smaller tarball files, where each contains files in the same base directory and named by the base directory.
This program is supposed to run together with tarball_divide, which divides a .tar.gz file into one or more smaller tarball files, where each contains files in the same base directory and named by the base directory.
tgz

Jump to

Keyboard shortcuts

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