sugarme / gotch

Go binding for Pytorch C++ API (libtorch)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

install error on osx

Arnold1 opened this issue · comments

Hi,

I have a small demo example I want to try but get the following error:

go run main.go
# github.com/sugarme/gotch/libtch
torch_api.cpp:1:9: fatal error: 'torch/csrc/autograd/engine.h' file not found

than I used: https://download.pytorch.org/libtorch/cpu/libtorch-macos-1.11.0.zip

and get this error:

go run main.go                                                                                                       

# github.com/sugarme/gotch/libtch
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:100:16: cannot use c_ptr (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:142:50: cannot use cindexes (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:151:49: cannot use cindexes (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration

@Arnold1,

Have you followed the instruction in README.md yet?

If you try to install on MacOSX, for gotch v0.7.0, please download libtorch CPU for Mac here.

  • Unzip to usr/local/lib
  • Update environment
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"
  • Then setup gotch for CPU:
wget https://raw.githubusercontent.com/sugarme/gotch/master/setup-gotch.sh
chmod +x setup-gotch.sh export CUDA_VER=cpu && export GOTCH_VER=v0.7.0 && bash setup-gotch.sh

If your your device is Mac M series, I have never tried on them, so I don't know whether it works or not. It would be great if you could report any errors in detail.

@sugarme updated my prev post... Its actually the intel Mac im using :)

now I get:

$ chmod +x setup-gotch.sh export CUDA_VER=cpu && export GOTCH_VER=v0.7.0 && bash setup-gotch.sh

sudo: ldconfig: command not found

so I replaced: sudo ldconfig with sudo update_dyld_shared_cache in the setup-gotch.sh

and get:

export CUDA_VER=cpu && export GOTCH_VER=v0.7.0 && bash setup-gotch.sh
GOPATH:'/Users/arnold1/go'
GOTCH_VERSION: 'v0.7.0'
CUDA_VERSION: 'cpu'
go: creating new go.mod: module github.com/sugarme/gotch-test
go: added github.com/sugarme/gotch v0.7.0
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/lib.go existing. Deleting...
creating /Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/lib.go for CPU
This tool is deprecated.

$ go run main.go                                                            
# github.com/sugarme/gotch/libtch
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:100:16: cannot use c_ptr (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:142:50: cannot use cindexes (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:151:49: cannot use cindexes (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration

@Arnold1 ,

Could you please try with clang compiler?

  • Install clang brew install --with-toolchain llvm
  • Set environment
export PATH="/usr/local/opt/llvm/bin:$PATH"
export CC := clang
export CXX := $(CC)++

@sugarme:

$ brew install --with-toolchain llvm
Error: invalid option: --with-toolchain

I already have clang btw:

clang --version
Apple clang version 13.0.0 (clang-1300.0.27.3)
Target: x86_64-apple-darwin21.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

@Arnold1

so just try

export CC := clang
export CXX := $(CC)++

to your .bashrc then rerun?

isn't that syntax for make?
export: not valid in this context: :

I think it needs to be:

$ export CC=/usr/bin/clang
$ export CXX=/usr/bin/clang++

@Arnold1 ,

And up to where your clang bin located.

@sugarme

setup-gotch.sh has: sudo ldconfig replaced with sudo update_dyld_shared_cache

I get:

$ export CC=/usr/bin/clang
$ export CXX=/usr/bin/clang++

$ export CUDA_VER=cpu && export GOTCH_VER=v0.7.0 && bash setup-gotch.sh 
GOPATH:'/Users/arnold1/go'
GOTCH_VERSION: 'v0.7.0'
CUDA_VERSION: 'cpu'
go: creating new go.mod: module github.com/sugarme/gotch-test
go: added github.com/sugarme/gotch v0.7.0
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/lib.go existing. Deleting...
creating /Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/lib.go for CPU
This tool is deprecated.

$ go run main.go                                                                              
# github.com/sugarme/gotch/libtch
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:100:16: cannot use c_ptr (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:142:50: cannot use cindexes (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration
/Users/arnold1/go/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/tensor.go:151:49: cannot use cindexes (variable of type *_Ctype_long) as type *_Ctype_longlong in variable declaration

here is my go example btw:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"

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

func main() {
    // Read the model file
    bytes, err := ioutil.ReadFile("../serve_model/model.pt")
    if err != nil {
        panic(err)
    }

    // Load the model
    model := gotch.Load(bytes).MustSequential()

    // Define a handler function
    http.HandleFunc("/predict", func(w http.ResponseWriter, r *http.Request) {
        // Parse the input parameters
        temp := gotch.NewTensor([]float32{r.FormValue("temp")}).MustFloat32()
        hum := gotch.NewTensor([]float32{r.FormValue("hum")}).MustFloat32()
        wind := gotch.NewTensor([]float32{r.FormValue("wind")}).MustFloat32()

        // Run the model
        input := nn.Cat([]*gotch.Tensor{temp, hum, wind}, 0)
        output := model.Forward(input).MustTensor()

        // Write the output to the response
        fmt.Fprintf(w, "The weather is predicted as: %v", output)
    })

    // Start the server
    http.ListenAndServe(":8080", nil)
}

@Arnold1 ,

I think it's an old issue that been reported here:

#44 (comment)

please try to update those 3 lines.

@sugarme yes - one step further:

$ go run main.go 
# github.com/sugarme/gotch/libtch
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:6:
In file included from /usr/local/lib/libtorch/include/ATen/Tensor.h:3:
In file included from /usr/local/lib/libtorch/include/ATen/core/TensorBody.h:18:
In file included from /usr/local/lib/libtorch/include/c10/core/ScalarTypeToTypeMeta.h:4:
In file included from /usr/local/lib/libtorch/include/c10/util/typeid.h:30:
/usr/local/lib/libtorch/include/c10/util/irange.h:19:32: warning: 'iterator<std::input_iterator_tag, unsigned long>' is deprecated [-Wdeprecated-declarations]
/usr/local/lib/libtorch/include/c10/util/irange.h:69:31: note: in instantiation of template class 'c10::detail::integer_iterator<unsigned long, 0>' requested here
/usr/local/lib/libtorch/include/c10/util/typeid.h:174:23: note: in instantiation of template class 'c10::integer_range<unsigned long, true>' requested here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::input_iterator_tag, unsigned long>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:6:
In file included from /usr/local/lib/libtorch/include/ATen/Tensor.h:3:
In file included from /usr/local/lib/libtorch/include/ATen/core/TensorBody.h:18:
In file included from /usr/local/lib/libtorch/include/c10/core/ScalarTypeToTypeMeta.h:4:
In file included from /usr/local/lib/libtorch/include/c10/util/typeid.h:30:
/usr/local/lib/libtorch/include/c10/util/irange.h:19:32: warning: 'iterator<std::input_iterator_tag, int>' is deprecated [-Wdeprecated-declarations]
/usr/local/lib/libtorch/include/c10/util/irange.h:69:31: note: in instantiation of template class 'c10::detail::integer_iterator<int, 0>' requested here
/usr/local/lib/libtorch/include/c10/core/TensorImpl.h:87:23: note: in instantiation of template class 'c10::integer_range<int, true>' requested here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::input_iterator_tag, int>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:7:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue.h:1272:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:8:
/usr/local/lib/libtorch/include/ATen/core/Dict.h:104:40: warning: 'iterator<std::forward_iterator_tag, c10::impl::DictEntryRef<c10::IValue, c10::IValue, ska_ordered::detailv3::sherwood_v3_table<std::pair<c10::IValue, c10::IValue>, c10::IValue, c10::detail::DictKeyHash, ska_ordered::detailv3::KeyOrValueHasher<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyHash>, c10::detail::DictKeyEqualTo, ska_ordered::detailv3::KeyOrValueEquality<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyEqualTo>, std::allocator<std::pair<c10::IValue, c10::IValue>>, std::allocator<ska_ordered::detailv3::sherwood_v3_entry<std::pair<c10::IValue, c10::IValue>>>>::templated_iterator<std::pair<c10::IValue, c10::IValue>>>>' is deprecated [-Wdeprecated-declarations]
/usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:1746:25: note: in instantiation of template class 'c10::impl::DictIterator<c10::IValue, c10::IValue, ska_ordered::detailv3::sherwood_v3_table<std::pair<c10::IValue, c10::IValue>, c10::IValue, c10::detail::DictKeyHash, ska_ordered::detailv3::KeyOrValueHasher<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyHash>, c10::detail::DictKeyEqualTo, ska_ordered::detailv3::KeyOrValueEquality<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyEqualTo>, std::allocator<std::pair<c10::IValue, c10::IValue>>, std::allocator<ska_ordered::detailv3::sherwood_v3_entry<std::pair<c10::IValue, c10::IValue>>>>::templated_iterator<std::pair<c10::IValue, c10::IValue>>>' requested here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::forward_iterator_tag, c10::impl::DictEntryRef<c10::IValue, c10::IValue, ska_ordered::detailv3::sherwood_v3_table<std::pair<c10::IValue, c10::IValue>, c10::IValue, c10::detail::DictKeyHash, ska_ordered::detailv3::KeyOrValueHasher<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyHash>, c10::detail::DictKeyEqualTo, ska_ordered::detailv3::KeyOrValueEquality<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyEqualTo>, std::allocator<std::pair<c10::IValue, c10::IValue>>, std::allocator<ska_ordered::detailv3::sherwood_v3_entry<std::pair<c10::IValue, c10::IValue>>>>::templated_iterator<std::pair<c10::IValue, c10::IValue>>>>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:7:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue.h:1272:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:8:
/usr/local/lib/libtorch/include/ATen/core/Dict.h:139:24: warning: 'iterator<std::random_access_iterator_tag, c10::impl::DictEntryRef<c10::IValue, c10::IValue, ska_ordered::detailv3::sherwood_v3_table<std::pair<c10::IValue, c10::IValue>, c10::IValue, c10::detail::DictKeyHash, ska_ordered::detailv3::KeyOrValueHasher<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyHash>, c10::detail::DictKeyEqualTo, ska_ordered::detailv3::KeyOrValueEquality<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyEqualTo>, std::allocator<std::pair<c10::IValue, c10::IValue>>, std::allocator<ska_ordered::detailv3::sherwood_v3_entry<std::pair<c10::IValue, c10::IValue>>>>::templated_iterator<std::pair<c10::IValue, c10::IValue>>>>' is deprecated [-Wdeprecated-declarations]
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::random_access_iterator_tag, c10::impl::DictEntryRef<c10::IValue, c10::IValue, ska_ordered::detailv3::sherwood_v3_table<std::pair<c10::IValue, c10::IValue>, c10::IValue, c10::detail::DictKeyHash, ska_ordered::detailv3::KeyOrValueHasher<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyHash>, c10::detail::DictKeyEqualTo, ska_ordered::detailv3::KeyOrValueEquality<c10::IValue, std::pair<c10::IValue, c10::IValue>, c10::detail::DictKeyEqualTo>, std::allocator<std::pair<c10::IValue, c10::IValue>>, std::allocator<ska_ordered::detailv3::sherwood_v3_entry<std::pair<c10::IValue, c10::IValue>>>>::templated_iterator<std::pair<c10::IValue, c10::IValue>>>>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:7:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue.h:1272:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:9:
/usr/local/lib/libtorch/include/ATen/core/List.h:106:40: warning: 'iterator<std::random_access_iterator_tag, c10::optional<at::Tensor>>' is deprecated [-Wdeprecated-declarations]
/usr/local/lib/libtorch/include/ATen/core/dispatch/DispatchKeyExtractor.h:71:40: note: in instantiation of template class 'c10::impl::ListIterator<c10::optional<at::Tensor>, std::__wrap_iter<c10::IValue *>>' requested here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::random_access_iterator_tag, c10::optional<at::Tensor>>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:7:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue.h:1272:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:9:
/usr/local/lib/libtorch/include/ATen/core/List.h:156:24: warning: 'iterator<std::random_access_iterator_tag, c10::optional<at::Tensor>>' is deprecated [-Wdeprecated-declarations]
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::random_access_iterator_tag, c10::optional<at::Tensor>>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:7:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue.h:1272:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:9:
/usr/local/lib/libtorch/include/ATen/core/List.h:106:40: warning: 'iterator<std::random_access_iterator_tag, at::Tensor>' is deprecated [-Wdeprecated-declarations]
/usr/local/lib/libtorch/include/ATen/core/dispatch/DispatchKeyExtractor.h:145:38: note: in instantiation of template class 'c10::impl::ListIterator<at::Tensor, std::__wrap_iter<c10::IValue *>>' requested here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::random_access_iterator_tag, at::Tensor>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:7:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue.h:1272:
In file included from /usr/local/lib/libtorch/include/ATen/core/ivalue_inl.h:9:
/usr/local/lib/libtorch/include/ATen/core/List.h:156:24: warning: 'iterator<std::random_access_iterator_tag, at::Tensor>' is deprecated [-Wdeprecated-declarations]
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::random_access_iterator_tag, at::Tensor>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
In file included from torch_api.cpp:1:
In file included from /usr/local/lib/libtorch/include/torch/csrc/autograd/engine.h:6:
In file included from /usr/local/lib/libtorch/include/ATen/Tensor.h:3:
In file included from /usr/local/lib/libtorch/include/ATen/core/TensorBody.h:18:
In file included from /usr/local/lib/libtorch/include/c10/core/ScalarTypeToTypeMeta.h:4:
In file included from /usr/local/lib/libtorch/include/c10/util/typeid.h:30:
/usr/local/lib/libtorch/include/c10/util/irange.h:19:32: warning: 'iterator<std::input_iterator_tag, long long>' is deprecated [-Wdeprecated-declarations]
/usr/local/lib/libtorch/include/c10/util/irange.h:69:31: note: in instantiation of template class 'c10::detail::integer_iterator<long long, 0>' requested here
/usr/local/lib/libtorch/include/ATen/WrapDimUtils.h:44:23: note: in instantiation of template class 'c10::integer_range<long long, true>' requested here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__iterator/iterator.h:27:29: note: 'iterator<std::input_iterator_tag, long long>' has been explicitly marked deprecated here
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1066:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__config:1043:48: note: expanded from macro '_LIBCPP_DEPRECATED'
# command-line-arguments
./main.go:20:20: undefined: gotch.Load
./main.go:25:23: undefined: gotch.NewTensor
./main.go:25:43: cannot use r.FormValue("temp") (value of type string) as type float32 in array or slice literal
./main.go:26:22: undefined: gotch.NewTensor
./main.go:26:42: cannot use r.FormValue("hum") (value of type string) as type float32 in array or slice literal
./main.go:27:23: undefined: gotch.NewTensor
./main.go:27:43: cannot use r.FormValue("wind") (value of type string) as type float32 in array or slice literal
./main.go:30:21: undefined: nn.Cat
./main.go:30:34: undefined: gotch.Tensor

@Arnold1 ,

It's not compiled because those lines are errors. We don't have such APIs.

./main.go:20:20: undefined: gotch.Load
./main.go:25:23: undefined: gotch.NewTensor
./main.go:25:43: cannot use r.FormValue("temp") (value of type string) as type float32 in array or slice literal
./main.go:26:22: undefined: gotch.NewTensor
./main.go:26:42: cannot use r.FormValue("hum") (value of type string) as type float32 in array or slice literal
./main.go:27:23: undefined: gotch.NewTensor
./main.go:27:43: cannot use r.FormValue("wind") (value of type string) as type float32 in array or slice literal
./main.go:30:21: undefined: nn.Cat
./main.go:30:34: undefined: gotch.Tensor
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"

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

func main() {
    // Read the model file
    bytes, err := ioutil.ReadFile("../serve_model/model.pt")
    if err != nil {
        panic(err)
    }

    // Load the model
    model := gotch.Load(bytes).MustSequential() // <--- Not gotch API

    // Define a handler function
    http.HandleFunc("/predict", func(w http.ResponseWriter, r *http.Request) {
        // Parse the input parameters
        temp := gotch.NewTensor([]float32{r.FormValue("temp")}).MustFloat32() // <--- Not API
        hum := gotch.NewTensor([]float32{r.FormValue("hum")}).MustFloat32()  // <-- Not API
        wind := gotch.NewTensor([]float32{r.FormValue("wind")}).MustFloat32() // <-- Not API

        // Run the model
        input := nn.Cat([]*gotch.Tensor{temp, hum, wind}, 0)  // <-- Not API
        output := model.Forward(input).MustTensor()  // <-- Not API

        // Write the output to the response
        fmt.Fprintf(w, "The weather is predicted as: %v", output)
    })

    // Start the server
    http.ListenAndServe(":8080", nil)
}

Please have a look at example folder for some common use cases.

You can run this code to see whether it compiles:

package main

import (
	"fmt"

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

func main() {
	x := ts.MustRandn([]int64{3, 224, 224}, gotch.Float, gotch.CPU)

	fmt.Printf("x: \n%0.4f\n", x)
}

// Output something like:
/*
x: 

(0, .,.) =
0.3179   0.9726   -1.0881  ..., -0.1043  -0.6456  -1.2466  
-0.1849  0.0189   2.3070   ..., 0.6651   1.1030   -1.5020  
0.3386   -0.0337  -1.1907  ..., 0.9963   -0.0966  -1.1827  
...,
-0.5802  -0.6195  -1.1689  ..., 0.3766   0.5937   -1.3450  
-1.2982  -1.2073  -0.4417  ..., 0.5881   0.5113   -0.9866  
0.5873   1.2127   -0.4048  ..., 0.3159   0.1075   -0.1914  

(1, .,.) =
-0.8650  1.3066   -0.0092  ..., 0.2856   -0.5619  1.4885   
-1.1424  -0.4724  0.4973   ..., -0.9246  -1.5363  -1.3134  
1.0577   0.4564   0.3045   ..., 0.2660   1.7841   -0.3224  
...,
0.5933   0.0881   1.0310   ..., 1.2442   0.4046   -0.0838  
2.4923   1.1102   1.0051   ..., 0.1855   -0.1949  1.1357   
-1.5688  -0.8963  -0.6812  ..., -0.8625  -0.3725  -1.3553  

(2, .,.) =
-0.0923  0.0816   -0.5417  ..., -0.0500  -0.6148  -0.6393  
0.5800   -0.2839  -0.0822  ..., 0.0119   -0.2610  -0.5950  
-0.8219  -0.3878  -0.0526  ..., -1.4697  0.1996   0.0333   
...,
0.6622   -0.2136  -0.0830  ..., 0.5019   0.1615   -1.4578  
0.9027   -0.4879  0.9262   ..., -0.1370  -0.4671  -0.5238  
-0.1661  -1.3880  -0.5563  ..., -0.5559  -1.8873  -1.4275  


*/

@sugarme I created a docker file:

Dockerfile:

FROM ubuntu:22.04

ENV DEBIAN_FRONTEND noninteractive

# Install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends build-essential ca-certificates cmake curl unzip nano wget g++

WORKDIR /home/developer
ENV HOME /home/developer

# Install Libtorch - CPU
RUN wget https://raw.githubusercontent.com/sugarme/gotch/master/setup-libtorch.sh
RUN chmod +x setup-libtorch.sh
RUN sed -i 's/sudo//g' setup-libtorch.sh
ENV CUDA_VER=cpu
RUN bash setup-libtorch.sh

ENV GOTCH_LIBTORCH="/usr/local/lib/libtorch"
ENV LIBRARY_PATH="$LIBRARY_PATH:$GOTCH_LIBTORCH/lib"
ENV CPATH="$CPATH:$GOTCH_LIBTORCH/lib:$GOTCH_LIBTORCH/include:$GOTCH_LIBTORCH/include/torch/csrc/api/include"
ENV LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GOTCH_LIBTORCH/lib"

# Setup gotch
RUN wget https://raw.githubusercontent.com/sugarme/gotch/master/setup-gotch.sh
RUN chmod +x setup-gotch.sh
RUN sed -i 's/sudo//g' setup-gotch.sh
ENV CUDA_VER=cpu
ENV GOTCH_VER=v0.7.0
RUN bash setup-gotch.sh

# Install golang
RUN wget -c https://go.dev/dl/go1.19.5.linux-amd64.tar.gz \
&& rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.5.linux-amd64.tar.gz \
&& rm go1.19.5.linux-amd64.tar.gz
ENV PATH=$PATH:/usr/local/go/bin

COPY main.go /home/developer/pytorch_demo/main.go

WORKDIR /home/developer/pytorch_demo
RUN go mod init main
RUN go mod tidy
RUN go build main.go

ENTRYPOINT bash

main.go

package main

import (
	"fmt"

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

func main() {
	x := ts.MustRandn([]int64{3, 224, 224}, gotch.Float, gotch.CPU)

	fmt.Printf("x: \n%0.4f\n", x)
}

// Output something like:
/*
x: 

(0, .,.) =
0.3179   0.9726   -1.0881  ..., -0.1043  -0.6456  -1.2466  
-0.1849  0.0189   2.3070   ..., 0.6651   1.1030   -1.5020  
0.3386   -0.0337  -1.1907  ..., 0.9963   -0.0966  -1.1827  
...,
-0.5802  -0.6195  -1.1689  ..., 0.3766   0.5937   -1.3450  
-1.2982  -1.2073  -0.4417  ..., 0.5881   0.5113   -0.9866  
0.5873   1.2127   -0.4048  ..., 0.3159   0.1075   -0.1914  

(1, .,.) =
-0.8650  1.3066   -0.0092  ..., 0.2856   -0.5619  1.4885   
-1.1424  -0.4724  0.4973   ..., -0.9246  -1.5363  -1.3134  
1.0577   0.4564   0.3045   ..., 0.2660   1.7841   -0.3224  
...,
0.5933   0.0881   1.0310   ..., 1.2442   0.4046   -0.0838  
2.4923   1.1102   1.0051   ..., 0.1855   -0.1949  1.1357   
-1.5688  -0.8963  -0.6812  ..., -0.8625  -0.3725  -1.3553  

(2, .,.) =
-0.0923  0.0816   -0.5417  ..., -0.0500  -0.6148  -0.6393  
0.5800   -0.2839  -0.0822  ..., 0.0119   -0.2610  -0.5950  
-0.8219  -0.3878  -0.0526  ..., -1.4697  0.1996   0.0333   
...,
0.6622   -0.2136  -0.0830  ..., 0.5019   0.1615   -1.4578  
0.9027   -0.4879  0.9262   ..., -0.1370  -0.4671  -0.5238  
-0.1661  -1.3880  -0.5563  ..., -0.5559  -1.8873  -1.4275  


*/

I get some coda errors - any idea why - bc I don't even use cuda!?

> [17/17] RUN go build main.go:
#21 107.2 # command-line-arguments
#21 107.2 /usr/local/go/pkg/tool/linux_amd64/link: running g++ failed: exit status 1
#21 107.2 /usr/bin/ld: cannot find -lcuda: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lcudart: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lcublas: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lcudnn: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lcaffe2_nvrtc: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lnvrtc-builtins: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lnvrtc: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lnvToolsExt: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -lc10_cuda: No such file or directory
#21 107.2 /usr/bin/ld: cannot find -ltorch_cuda: No such file or directory
#21 107.2 collect2: error: ld returned 1 exit status
#21 107.2 

@Arnold1,

That's error because your Dockerfile didn't effectively update CGO flags at gotch package $GOPATH/$GOPATH/pkg/mod/github.com/sugarme/gotch@$GOTCH_VERSION/libtch/lib.go .

In your Dockerfile you should do these in order:

  1. Install compiler dependencies
  2. Install Go and setup GOPATH environment
  3. Install libtorch
  4. Install gotch

Have a look at Setup gotch CPU shell script for more detail (it needs to read GOPATH env to know where is gotch package located and update its lib.go file for CGO flags before your Go example can compile and run).

Hope that helps.

If you get your MacOS setup and working, please close this #86 issue and open a new one please. Thanks.

I move the linux problem to a new issue - and only report the osx related problem here...

@Arnold1,

Cgo flags located at

$GOPATH/pkg/mod/github.com/sugarme/gotch@v0.7.0/libtch/lib.go

Compilers can be either gcc or clang.

You don't need to set cgo flag for your own projects.

will close that ticket for now.