hasktorch/hasktorch
hasktorch/hasktorch has 1,215 GitHub stars and 127 forks. GitHot measured 0 stars in 24 hours, -2 in 7 days, and -2 in 15 days. Last checked 2026-11-21 12:18 UTC. Last push 2026-10-20.
Progression mesurée des étoiles
- in 24h
- 0
- in 7 days
- -2
- in 15 days
- -2
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
À propos
Tensors and neural networks in Haskell
Apprentissage automatiqueREADME.md
OriginalHasktorch
Hasktorch is a library for tensors and neural networks in Haskell. It is an independent open source community project which leverages the core C++ libraries shared by PyTorch.
This project is in active development, so expect changes to the library API as it evolves. We would like to invite new users to join our Hasktorch discord space for questions and discussions. Contributions/PR are encouraged.
The second major release of Hasktorch (0.2) is currently available on hackage and nixpkgs.
Documentation
The documentation is divided into several sections:
Introductory Videos
- High-level MuniHac talk by @austinvhuang
- Hands-on live-coding demo by @tscholak
- Low-level FFI talk by @junjihashimoto
Getting Started
Using Hasktorch in your own project — no manual installation
You do not need to install libtorch (or anything else) by hand.
Add hasktorch to your project's build-depends:
build-depends: base, hasktorch
The first time you run cabal build, the build downloads the
official libtorch binaries from download.pytorch.org automatically
and caches them under ~/.cache/libtorch/<version>/<platform>/<flavor>.
Later builds — and every other project on the machine — reuse that
cache; nothing is downloaded twice. The linker rpath is set for you,
so the built binaries find the libraries at runtime without any
environment variables.
Supported out of the box:
| Platform | Backend |
|---|---|
| Linux x86_64 | CPU (default), or CUDA with export LIBTORCH_CUDA_VERSION=cu118 (etc.) before building — the needed NVIDIA libraries are downloaded automatically too |
| macOS Apple Silicon | CPU; the binary also includes Apple's MPS backend (Device MPS 0) |
| macOS Intel | CPU |
Environment variables, all optional: LIBTORCH_VERSION (pin a
libtorch version), LIBTORCH_HOME (cache location),
LIBTORCH_CUDA_VERSION (cpu, cu118, cu121, …), and
LIBTORCH_SKIP_DOWNLOAD (use a system-provided libtorch instead).
Nix builds skip the download; the flake provides libtorch itself.
Working on hasktorch itself
The following steps assume the hasktorch repository has just been cloned. After setup is done, read the online tutorials and API documents.
- linux+cabal+cpu
- linux+cabal+cuda11
- macos+cabal+cpu
- linux+stack+cpu
- macos+stack+cpu
- nixos+cabal+cpu
- nixos+cabal+cuda11
- docker+jupyterlab+cuda11
linux+cabal+cpu
Starting from the top-level directory of the project, run:
$ ./setup-cabal.sh # Create a cabal project file
To build and test the Hasktorch library, run:
$ cabal build hasktorch # Build the Hasktorch library.
$ cabal test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ cabal build examples # Build the Hasktorch examples.
$ cabal test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE=cpu # Set device to CPU for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
linux+cabal+cuda
First set the LIBTORCH_CUDA_VERSION environment variable to
the CUDA version you're currently using. E.g
export LIBTORCH_CUDA_VERSION=cu118 for CUDA 11.8.
If unspecified, Hasktorch defaults to downloading the cpu version.
Starting from the top-level directory of the project, run:
$ ./setup-cabal.sh # Create a cabal project file
To build and test the Hasktorch library, run:
$ cabal build hasktorch # Build the Hasktorch library.
$ cabal test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ cabal build examples # Build the Hasktorch examples.
$ cabal test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE="cuda:0" # Set device to CUDA for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
macos+cabal+cpu
Starting from the top-level directory of the project, run:
$ ./setup-cabal.sh # Create a cabal project file
To build and test the Hasktorch library, run:
$ cabal build hasktorch # Build the Hasktorch library.
$ cabal test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ cabal build examples # Build the Hasktorch examples.
$ cabal test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE=cpu # Set device to CPU for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
linux+stack+cpu
Install the Haskell Tool Stack if you haven't already, following instructions here
To build and test the Hasktorch library, run:
$ stack build hasktorch # Build the Hasktorch library.
$ stack test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ stack build examples # Build the Hasktorch examples.
$ stack test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE=cpu # Set device to CPU for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
macos+stack+cpu
Install the Haskell Tool Stack if you haven't already, following instructions here
To build and test the Hasktorch library, run:
$ stack build hasktorch # Build the Hasktorch library.
$ stack test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ stack build examples # Build the Hasktorch examples.
$ stack test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE=cpu # Set device to CPU for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
nixos+cabal+cpu
(Optional) Install and set up Cachix:
$ nix-env -iA cachix -f https://cachix.org/api/v1/install # (Optional) Install Cachix.
# (Optional) Use IOHK's cache. See https://input-output-hk.github.io/haskell.nix/tutorials/getting-started/#setting-up-the-binary-cache
$ cachix use hasktorch # (Optional) Use hasktorch's cache.
Starting from the top-level directory of the project, run:
$ nix develop # Enter the nix shell environment for Hasktorch.
To build and test the Hasktorch library, run:
$ cabal build hasktorch # Build the Hasktorch library.
$ cabal test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ cabal build examples # Build the Hasktorch examples.
$ cabal test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE=cpu # Set device to CPU for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
nixos+cabal+cuda11
(Optional) Install and set up Cachix:
$ nix-env -iA cachix -f https://cachix.org/api/v1/install # (Optional) Install Cachix.
# (Optional) Use IOHK's cache. See https://input-output-hk.github.io/haskell.nix/tutorials/getting-started/#setting-up-the-binary-cache
$ cachix use hasktorch # (Optional) Use hasktorch's cache.
Starting from the top-level directory of the project, run:
$ cat > nix/dev-config.nix
{
profiling = true;
cudaSupport = true;
cudaMajorVersion = "11";
}
$ nix develop # Enter the nix shell environment for Hasktorch.
To build and test the Hasktorch library, run:
$ cabal build hasktorch # Build the Hasktorch library.
$ cabal test hasktorch # Build and run the Hasktorch library test suite.
To build and test the example executables shipped with hasktorch, run:
$ cabal build examples # Build the Hasktorch examples.
$ cabal test examples # Build and run the Hasktorch example test suites.
To run the MNIST CNN example, run:
$ cd examples # Change to the examples directory.
$ ./datasets/download-mnist.sh # Download the MNIST dataset.
$ export DEVICE="cuda:0" # Set device to CUDA for the MNIST CNN example.
$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.
docker+jupyterlab+cuda11
This dockerhub repository provides the docker-image of jupyterlab. It supports cuda11, cuda10 and cpu only. When you use jupyterlab with hasktorch, type following command, then click a url in a console.
$ docker run --gpus all -it --rm -p 8888:8888 htorch/hasktorch-jupyter
or
$ docker run --gpus all -it --rm -p 8888:8888 htorch/hasktorch-jupyter:latest-cu11
Known Issues
MPS (Metal Performance Shaders) Support on macOS
When using Hasktorch on Apple Silicon Macs with MPS device support, you must set the PYTORCH_ENABLE_MPS_FALLBACK=1 environment variable before running your program:
$ export PYTORCH_ENABLE_MPS_FALLBACK=1
$ cabal test hasktorch # Run tests with MPS fallback enabled
Or run directly with:
$ PYTORCH_ENABLE_MPS_FALLBACK=1 cabal test hasktorch
This is required because some operations (like erfc) are not yet natively implemented for the MPS backend in PyTorch and need to fall back to CPU computation. Without this environment variable, you will see errors like:
The operator 'aten::erfc.out' is not currently implemented for the MPS device.
Note: When this environment variable is not set, MPS tests will be automatically skipped to prevent failures.
Tensors Cannot Be Moved to CUDA
In rare cases, you may see errors like
cannot move tensor to "CUDA:0"
although you have CUDA capable hardware in your machine and have followed the getting-started instructions for CUDA support.
If that happens, check if /run/opengl-driver/lib exists.
If not, make sure your CUDA drivers are installed correctly.
Weird Behaviour When Switching from CPU-Only to CUDA-Enabled Nix Shell
If you have run cabal in a CPU-only Hasktorch Nix shell before,
you may need to:
- Clean the
dist-newstylefolder usingcabal clean. - Delete the
.ghc.environment*file in the Hasktorch root folder.
Otherwise, at best, you will not be able to move tensors to CUDA, and, at worst, you will see weird linker errors like
gcc: error: hasktorch/dist-newstyle/build/x86_64-linux/ghc-8.8.3/libtorch-ffi-1.5.0.0/build/Torch/Internal/Unmanaged/Autograd.dyn_o: No such file or directory
`cc' failed in phase `Linker'. (Exit code: 1)
Contributing
We welcome new contributors.
Contact us for access to the hasktorch discord channel. You can send an email to [email protected] or on twitter as @austinvhuang, @SamStites, @tscholak, or @junjihashimoto3.
Notes for library developers
See the wiki for developer notes.
Project Folder Structure
Basic functionality:
deps/- submodules and downloads for build dependencies (libtorch, mklml, pytorch) -- you can ignore this if you are on Nixexamples/- high level example models (xor mlp, typed cnn, etc.)experimental/- experimental projects or tipshasktorch/- higher level user-facing library, calls intoffi/, used byexamples/
Internals (for contributing developers):
codegen/- code generation, parsesDeclarations.yamlspec from pytorch and producesffi/contentsinline-c/- submodule to inline-cpp fork used for C++ FFIlibtorch-ffi/- low level FFI bindings to libtorchspec/- specification files used forcodegen/