Coder Social home page Coder Social logo

go-torch's Introduction

EVERYHING IS REALLY OUT-OF-DATE. FOR A MORE UP-TO-DATE BINDINGS I RECOMMEND CHECKING OUT https://github.com/wangkuiyi/gotorch OR OTHER PACKAGES AVAILABLE.

WORK IN PROGRESS... USE AT OWN RISK :-)

Build Status GoDoc

go-torch

LibTorch (PyTorch) bindings for Golang. Library is first and foremost designed for running inference against serialized models exported from Python version of PyTorch. Library can also be used to compile TorchScript applications directly from Go.

Installing

$ go get github.com/orktes/go-torch

Usage

go-torch depends on the LibTorch shared library to be available. For more information refer to https://pytorch.org/cppdocs/. The is also an example Dockerfile which is used for executing tests for the library.

import (
    "github.com/orktes/go-torch"
)

Creating Tensors

Supported scalar types:

  • torch.Byte uint8
  • torch.Char int8
  • torch.Int int32
  • torch.Long int64
  • torch.Float float32
  • torch.Double float64
matrix := []float32{
    []float32{1,2,3},
    []float32{4,5,6},
}
tensor, _ := torch.NewTensor(matrix)
tensor.Shape() // [2, 3]
tensor.DType() // torch.Float

Using serialized PyTorch models

For instructions on how to export models for PyTorch refer to the PyTorch documentation

// Load model
module, _ := torch.LoadJITModule("model.pt")

// Create an input tensor
inputTensor, _ := torch.NewTensor([][]float32{
    []float32{1, 2, 3},
})

// Forward propagation
res, _ := module.Forward(inputTensor)

Using TorchScript

TorchScript documentation

Currently supported input and output types

  • Tensor
  • Tuple (of Tensor and/or nested Tuples)
sumScript = `
def sum(a, b):
    return a + b
`

// Compile TorchScript
module, _ := torch.CompileTorchScript(sumScript)

// Create inputs
a, _ := torch.NewTensor([]float32{1})
b, _ := torch.NewTensor([]float32{2})

res, _ := module.RunMethod("sum", a, b)
fmt.Printf("[1] + [2] = %+v\n", res.(*torch.Tensor).Value())
// output: [1] + [2] = [3]

Acknowledgements

Lots of the functionality related to converting Golang types to PyTorch Tensors are a shameless copy on what Google is doing with their Go Tensorflow bindings. Therefore big part of the credit definetely goes to The TensorFlow Authors.

LICENSE

See here

go-torch's People

Contributors

orktes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-torch's Issues

Up to date instructions to build the package

Hi,
I'm interested to try out this package but I find that build instructions are out of date. I tried to setup docker with recent version of libtorch library https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.8.1%2Bcpu.zip or https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.8.1%2Bcpu.zip but go get fails on multiple fronts. For example, the libtorch library has different structure of includes now, e.g. torch/torch.h is hidden within csrc area, etc. Even if I copy everything to /usr/local the go get complains about #error C++14 or later compatible compiler is required to use PyTorch (even though docker has recent g++). There are lots of errors from c10, etc.

I'll appreciate if proper set of instruction how to build torch.cpp (supplied in this package) will be provided in order to build go based code.

How to use it in macOS

github.com/orktes/go-torch

torch.cpp:3:10: fatal error: 'torch/torch.h' file not found

The Docker image doesn't run

I get these (and a large number of errors afterwards):

17:37 $ docker-compose up test
Creating scripts_test_1 ... done
Attaching to scripts_test_1
test_1       | # github.com/orktes/go-torch
test_1       | torch.cpp: In function 'c10::IValue Torch_ConvertTorchIValueToIValue(Torch_IValue)':
test_1       | torch.cpp:160:28: error: 'torch::jit::Tuple' has not been declared
test_1       |          return torch::jit::Tuple::create(std::move(values));
test_1       |                             ^~~~~
test_1       | torch.cpp: In function 'void* Torch_LoadJITModule(char*, Torch_Error*)':
test_1       | torch.cpp:226:47: error: no match for 'operator=' (operand types are 'std::shared_ptr<torch::jit::script::Module>' and 'torch::jit::script::Module')
test_1       |      mod->module = torch::jit::load(module_path);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.