Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gotch

> 编程语言
Open source

Go binding for Pytorch C++ API (libtorch)

652 stars0 likes1 views
WebsiteGitHub

About

Go binding for Pytorch C++ API (libtorch)

Gotch

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!)

bash
    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

bash
    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

bash
    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.

  • Check version: nvidia-smi
  • Install nvidia driver here
  • Install CUDA here
  • Install CuDNN here

Step 1: Setup libtorch (skip this step if a valid libtorch already installed in your machine!)

bash
    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

bash
    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

bash
    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

…

Simplified Convolutional neural network

…

Play with gotch on Google Colab or locally

  • Tensor Initiation
  • Tensor Indexing
  • MNIST
  • YOLO v3 model infering
  • RNN model training
  • CIFAR model training
  • JIT ResNet18 Torch Script model load and inference
  • Neural style transfer
  • Image pretrained models - inference
  • Translation
  • Convert Pytorch Python model to Go
  • Load Python Pytorch JIT model then train/finetune in Go
  • Image augmentation

Getting Started

  • See pkg.go.dev for APIs detail.

License

gotch is Apache 2.0 licensed.

Acknowledgement

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Godeep-learningframeworkgogo-binding

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言