gotch

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

Fork of gotch modified to work with my fyp project

Gotch LicenseGo.Dev reference

Overview

gotch creates a thin wrapper to Pytorch C++ APIs (Libtorch) to make use of its already optimized C++ tensor APIs (3039) and dynamic graph computation with CUDA support and provides idiomatic Go APIs for developing and implementing Deep Learning in Go.

Some features are

  • Comprehensive Pytorch tensor APIs (2525)
  • Fully featured Pytorch dynamic graph computation
  • JIT interface to run model trained/saved using PyTorch Python API
  • Load pretrained Pytorch models and run inference
  • Pure Go APIs to build and train neural network models with both CPU and GPU support
  • Most recent image models
  • NLP Language models - Transformer in separate package built with gotch and pure Go Tokenizer.

gotch is in active development mode and may have API breaking changes. Feel free to pull request, report issues or discuss any concerns. All contributions are welcome.

gotch current version is v0.9.1

Dependencies

  • Libtorch C++ v2.1.0 library of Pytorch
  • Clang-17/Clang++-17 compilers

Installation

  • Default CUDA version is 11.8 if CUDA is available otherwise using CPU version.
  • Default Pytorch C++ API version is 2.1.0

NOTE: libtorch will be installed at /usr/local/lib

CPU
Step 1: Setup libtorch (skip this step if a valid libtorch already installed in your machine!)
    wget https://github.com/sugarme/gotch/releases/download/v0.9.0/setup-libtorch.sh
    chmod +x setup-libtorch.sh
    export CUDA_VER=cpu && bash setup-libtorch.sh

Update Environment: in Debian/Ubuntu, add/update the following lines to .bashrc file

    export GOTCH_LIBTORCH="/usr/local/lib/libtorch"
    export LIBRARY_PATH="$LIBRARY_PATH:$GOTCH_LIBTORCH/lib"
    export CPATH="$CPATH:$GOTCH_LIBTORCH/lib:$GOTCH_LIBTORCH/include:$GOTCH_LIBTORCH/include/torch/csrc/api/include"
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GOTCH_LIBTORCH/lib"
Step 2: Setup gotch
    wget https://github.com/sugarme/gotch/releases/download/v0.9.0/setup-gotch.sh
    chmod +x setup-gotch.sh
    export CUDA_VER=cpu && export GOTCH_VER=v0.9.1 && bash setup-gotch.sh
GPU

NOTE: make sure your machine has working CUDA.

Step 1: Setup libtorch (skip this step if a valid libtorch already installed in your machine!)
    wget https://github.com/sugarme/gotch/releases/download/v0.9.0/setup-libtorch.sh
    chmod +x setup-libtorch.sh

    export CUDA_VER=11.8 && bash setup-libtorch.sh

Update Environment: in Debian/Ubuntu, add/update the following lines to .bashrc file

    export GOTCH_LIBTORCH="/usr/local/lib/libtorch"
    export LIBRARY_PATH="$LIBRARY_PATH:$GOTCH_LIBTORCH/lib"
    export CPATH="$CPATH:$GOTCH_LIBTORCH/lib:$GOTCH_LIBTORCH/include:$GOTCH_LIBTORCH/include/torch/csrc/api/include"
    LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GOTCH_LIBTORCH/lib:/usr/lib64-nvidia:/usr/local/cuda-${CUDA_VERSION}/lib64"
Step 2: Setup gotch
    wget https://github.com/sugarme/gotch/releases/download/v0.9.0/setup-gotch.sh
    chmod +x setup-gotch.sh
    export CUDA_VER=11.8 && export GOTCH_VER=v0.9.1 && bash setup-gotch.sh

Examples

Basic tensor operations
import (
	"fmt"

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

func basicOps() {

xs := ts.MustRand([]int64{3, 5, 6}, gotch.Float, gotch.CPU)
fmt.Printf("%8.3f\n", xs)
fmt.Printf("%i", xs)

/*
(1,.,.) =
   0.391     0.055     0.638     0.514     0.757     0.446  
   0.817     0.075     0.437     0.452     0.077     0.492  
   0.504     0.945     0.863     0.243     0.254     0.640  
   0.850     0.132     0.763     0.572     0.216     0.116  
   0.410     0.660     0.156     0.336     0.885     0.391  

(2,.,.) =
   0.952     0.731     0.380     0.390     0.374     0.001  
   0.455     0.142     0.088     0.039     0.862     0.939  
   0.621     0.198     0.728     0.914     0.168     0.057  
   0.655     0.231     0.680     0.069     0.803     0.243  
   0.853     0.729     0.983     0.534     0.749     0.624  

(3,.,.) =
   0.734     0.447     0.914     0.956     0.269     0.000  
   0.427     0.034     0.477     0.535     0.440     0.972  
   0.407     0.945     0.099     0.184     0.778     0.058  
   0.482     0.996     0.085     0.605     0.282     0.671  
   0.887     0.029     0.005     0.216     0.354     0.262  



TENSOR INFO:
        Shape:          [3 5 6]
        DType:          float32
        Device:         {CPU 1}
        Defined:        true
*/

// 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.Printf("ts1:\n%2d", ts1)
fmt.Printf("ts2:\n%2d", ts2)
fmt.Printf("mul tensor (ts1 x ts2):\n%2d", mul)

/*
ts1:
 0   1   2  
 3   4   5  

ts2:
 1   1   1   1  
 1   1   1   1  
 1   1   1   1  

mul tensor (ts1 x ts2):
 3   3   3   3  
12  12  12  12  
*/


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

/*
Before:
1  1  1  
1  1  1  

After (ts3 + 2.0):
3  3  3  
3  3  3  
*/
}
Simplified Convolutional neural network
import (
    "fmt"

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

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) *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)
    fmt.Printf("Logits: %0.3f", logits)
}

//Logits: 0.000  0.000  0.000  0.225  0.321  0.147  0.000  0.207  0.000  0.000

Play with gotch on Google Colab or locally

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

View Source
const (
	// NOTE. reflect.Kind 0-26
	QUInt8Kind      reflect.Kind = 27
	QInt8Kind       reflect.Kind = 28
	QInt32Kind      reflect.Kind = 29
	Float16Kind     reflect.Kind = 30
	BFloat16Kind    reflect.Kind = 31
	QUInt4x2Kind    reflect.Kind = 32
	QUInt2x4Kind    reflect.Kind = 33
	Bits1x8Kind     reflect.Kind = 34
	Bits2x4Kind     reflect.Kind = 35
	Bits4x2Kind     reflect.Kind = 36
	Bits8Kind       reflect.Kind = 37
	Bits16Kind      reflect.Kind = 38
	ComplexHalfKind reflect.Kind = 39
)

Variables

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

	Debug bool = false
)
View Source
var ModelUrls map[string]string = map[string]string{
	"alexnet": "https://download.pytorch.org/models/alexnet-owt-7be5be79.pth",

	"convnext_tiny":  "https://download.pytorch.org/models/convnext_tiny-983f1562.pth",
	"convnext_small": "https://download.pytorch.org/models/convnext_small-0c510722.pth",
	"convnext_base":  "https://download.pytorch.org/models/convnext_base-6075fbad.pth",
	"convnext_large": "https://download.pytorch.org/models/convnext_large-ea097f82.pth",

	"densenet121": "https://download.pytorch.org/models/densenet121-a639ec97.pth",
	"densenet169": "https://download.pytorch.org/models/densenet169-b2777c0a.pth",
	"densenet201": "https://download.pytorch.org/models/densenet201-c1103571.pth",
	"densenet161": "https://download.pytorch.org/models/densenet161-8d451a50.pth",

	"efficientnet_b0": "https://download.pytorch.org/models/efficientnet_b0_rwightman-3dd342df.pth",
	"efficientnet_b1": "https://download.pytorch.org/models/efficientnet_b1_rwightman-533bc792.pth",
	"efficientnet_b2": "https://download.pytorch.org/models/efficientnet_b2_rwightman-bcdf34b7.pth",
	"efficientnet_b3": "https://download.pytorch.org/models/efficientnet_b3_rwightman-cf984f9c.pth",
	"efficientnet_b4": "https://download.pytorch.org/models/efficientnet_b4_rwightman-7eb33cd5.pth",

	"efficientnet_b5": "https://download.pytorch.org/models/efficientnet_b5_lukemelas-b6417697.pth",
	"efficientnet_b6": "https://download.pytorch.org/models/efficientnet_b6_lukemelas-c76e70fd.pth",
	"efficientnet_b7": "https://download.pytorch.org/models/efficientnet_b7_lukemelas-dcc49843.pth",

	"googlenet": "https://download.pytorch.org/models/googlenet-1378be20.pth",

	"inception_v3_google": "https://download.pytorch.org/models/inception_v3_google-0cc3c7bd.pth",

	"mnasnet0_5":  "https://download.pytorch.org/models/mnasnet0.5_top1_67.823-3ffadce67e.pth",
	"mnasnet0_75": "",
	"mnasnet1_0":  "https://download.pytorch.org/models/mnasnet1.0_top1_73.512-f206786ef8.pth",
	"mnasnet1_3":  "",

	"mobilenet_v2":       "https://download.pytorch.org/models/mobilenet_v2-b0353104.pth",
	"mobilenet_v3_large": "https://download.pytorch.org/models/mobilenet_v3_large-8738ca79.pth",
	"mobilenet_v3_small": "https://download.pytorch.org/models/mobilenet_v3_small-047dcff4.pth",

	"regnet_y_400mf": "https://download.pytorch.org/models/regnet_y_400mf-c65dace8.pth",
	"regnet_y_800mf": "https://download.pytorch.org/models/regnet_y_800mf-1b27b58c.pth",
	"regnet_y_1_6gf": "https://download.pytorch.org/models/regnet_y_1_6gf-b11a554e.pth",
	"regnet_y_3_2gf": "https://download.pytorch.org/models/regnet_y_3_2gf-b5a9779c.pth",
	"regnet_y_8gf":   "https://download.pytorch.org/models/regnet_y_8gf-d0d0e4a8.pth",
	"regnet_y_16gf":  "https://download.pytorch.org/models/regnet_y_16gf-9e6ed7dd.pth",
	"regnet_y_32gf":  "https://download.pytorch.org/models/regnet_y_32gf-4dee3f7a.pth",
	"regnet_x_400mf": "https://download.pytorch.org/models/regnet_x_400mf-adf1edd5.pth",
	"regnet_x_800mf": "https://download.pytorch.org/models/regnet_x_800mf-ad17e45c.pth",
	"regnet_x_1_6gf": "https://download.pytorch.org/models/regnet_x_1_6gf-e3633e7f.pth",
	"regnet_x_3_2gf": "https://download.pytorch.org/models/regnet_x_3_2gf-f342aeae.pth",
	"regnet_x_8gf":   "https://download.pytorch.org/models/regnet_x_8gf-03ceed89.pth",
	"regnet_x_16gf":  "https://download.pytorch.org/models/regnet_x_16gf-2007eb11.pth",
	"regnet_x_32gf":  "https://download.pytorch.org/models/regnet_x_32gf-9d47f8d0.pth",

	"resnet18":         "https://download.pytorch.org/models/resnet18-f37072fd.pth",
	"resnet34":         "https://download.pytorch.org/models/resnet34-b627a593.pth",
	"resnet50":         "https://download.pytorch.org/models/resnet50-0676ba61.pth",
	"resnet101":        "https://download.pytorch.org/models/resnet101-63fe2227.pth",
	"resnet152":        "https://download.pytorch.org/models/resnet152-394f9c45.pth",
	"resnext50_32x4d":  "https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth",
	"resnext101_32x8d": "https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth",
	"wide_resnet50_2":  "https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth",
	"wide_resnet101_2": "https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth",

	"shufflenetv2_x0.5": "https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth",
	"shufflenetv2_x1.0": "https://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth",
	"shufflenetv2_x1.5": "",
	"shufflenetv2_x2.0": "",

	"squeezenet1_0": "https://download.pytorch.org/models/squeezenet1_0-b66bff10.pth",
	"squeezenet1_1": "https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth",

	"vgg11":    "https://download.pytorch.org/models/vgg11-8a719046.pth",
	"vgg13":    "https://download.pytorch.org/models/vgg13-19584684.pth",
	"vgg16":    "https://download.pytorch.org/models/vgg16-397923af.pth",
	"vgg19":    "https://download.pytorch.org/models/vgg19-dcbb9e9d.pth",
	"vgg11_bn": "https://download.pytorch.org/models/vgg11_bn-6002323d.pth",
	"vgg13_bn": "https://download.pytorch.org/models/vgg13_bn-abd245e5.pth",
	"vgg16_bn": "https://download.pytorch.org/models/vgg16_bn-6c64b313.pth",
	"vgg19_bn": "https://download.pytorch.org/models/vgg19_bn-c79401a0.pth",

	"vit_b_16": "https://download.pytorch.org/models/vit_b_16-c867db91.pth",
	"vit_b_32": "https://download.pytorch.org/models/vit_b_32-d86f8d99.pth",
	"vit_l_16": "https://download.pytorch.org/models/vit_l_16-852ce7e3.pth",
	"vit_l_32": "https://download.pytorch.org/models/vit_l_32-c7638314.pth",
}

ModelUrls maps model name to its pretrained URL.

This URLS taken from separate models in pytorch/vision repository https://github.com/pytorch/vision/tree/main/torchvision/models

Functions

func CachedPath

func CachedPath(filenameOrUrl string, folderOpt ...string) (resolvedPath string, err error)

CachedPath resolves and caches data based on input string, then returns fullpath to the cached data.

Parameters: - `filenameOrUrl`: full path to filename or url

CachedPath does several things consequently: 1. Resolves input string to a fullpath cached filename candidate. 2. Check it at `CachedDir`, if exists, then return the candidate. If not 3. Retrieves and Caches data to `CachedDir` and returns path to cached data

func CleanCache

func CleanCache() error

CleanCache removes all files cached at `CachedDir`

func IsFloatDType

func IsFloatDType(dtype DType) bool

IsFloatDType returns whether dtype is floating point data type.

func PrintMemStats

func PrintMemStats(messageOpt ...string)

Types

type CInt

type CInt = int32

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

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 int

DType represents different kind of element that a tensor can hold. Ref. https://github.com/pytorch/pytorch/blob/a290cbf32b0c282aa60fa521ca5c6cd19c7f779f/c10/core/ScalarType.h

const (
	Invalid       DType = -1
	Uint8         DType = 0
	Int8          DType = 1
	Int16         DType = 2
	Int           DType = 3
	Int64         DType = 4
	Half          DType = 5
	Float         DType = 6
	Double        DType = 7
	ComplexHalf   DType = 8
	ComplexFloat  DType = 9
	ComplexDouble DType = 10
	Bool          DType = 11
	QInt8         DType = 12
	QUInt8        DType = 13
	QInt32        DType = 14
	BFloat16      DType = 15
	// ---not implemented ---
	QUInt4x2 DType = 16
	QUInt2x4 DType = 17
	Bits1x8  DType = 18
	Bits2x4  DType = 19
	Bits4x2  DType = 20
	Bits8    DType = 21
	Bits16   DType = 22
)
var DefaultDType DType = Float

Default DType: ==============

func CKind2DType

func CKind2DType(ckind int32) DType

func DTypeFromData

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

func GoKind2DType

func GoKind2DType(kind reflect.Kind, opts ...DTypeOpt) (DType, error)

func SetDefaultDType

func SetDefaultDType(dtype DType) DType

SetDefaultDType set DefaultDType to new value and return the previous one.

func (DType) CInt

func (dt DType) CInt() CInt

Back compat

func (DType) CKind

func (dt DType) CKind() CInt

func (DType) GoKind

func (dt DType) GoKind() reflect.Kind

func (DType) GoType

func (dt DType) GoType() (reflect.Type, error)

func (DType) Size

func (dt DType) Size() uint

Size returns dtype size in Bytes.

func (DType) String

func (dt DType) String() string

type DTypeDevice

type DTypeDevice struct {
	DType  DType
	Device Device
}

type DTypeOpt

type DTypeOpt func(*DTypeOptions)

func HalfDTypePref

func HalfDTypePref(v DType) DTypeOpt

func WithQuantized

func WithQuantized(v bool) DTypeOpt

type DTypeOptions

type DTypeOptions struct {
	HalfDTypePref DType
	Quantized     bool
}

func DefaultDTypeOptions

func DefaultDTypeOptions() *DTypeOptions

type Device

type Device struct {
	Name  string
	Value int
}

func CudaBuilder

func CudaBuilder(v uint) Device

func CudaIfAvailable

func CudaIfAvailable() Device

CudaIfAvailable returns a GPU device if available, else CPU.

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
mem
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.
Package half defines support for half-precision floating-point numbers.
Package half defines support for half-precision floating-point numbers.
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`
aug

Jump to

Keyboard shortcuts

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