{"id":13541461,"url":"https://github.com/hasktorch/hasktorch","last_synced_at":"2025-05-14T22:08:01.989Z","repository":{"id":40433965,"uuid":"102987059","full_name":"hasktorch/hasktorch","owner":"hasktorch","description":"Tensors and neural networks in Haskell","archived":false,"fork":false,"pushed_at":"2025-04-19T04:36:47.000Z","size":14027,"stargazers_count":1109,"open_issues_count":89,"forks_count":114,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-04-19T12:38:50.888Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://hasktorch.org","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hasktorch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-09-09T21:50:59.000Z","updated_at":"2025-04-19T07:12:56.000Z","dependencies_parsed_at":"2023-12-28T00:00:35.694Z","dependency_job_id":"9c0757a5-25d2-494d-9195-70c6a1947ea0","html_url":"https://github.com/hasktorch/hasktorch","commit_stats":{"total_commits":3582,"total_committers":43,"mean_commits":83.30232558139535,"dds":0.7585147962032384,"last_synced_commit":"7218e00e1fb3b6e85b7f655aa1ba56418f636d37"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasktorch%2Fhasktorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasktorch%2Fhasktorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasktorch%2Fhasktorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasktorch%2Fhasktorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hasktorch","download_url":"https://codeload.github.com/hasktorch/hasktorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254235696,"owners_count":22036963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T10:00:48.208Z","updated_at":"2025-05-14T22:07:56.976Z","avatar_url":"https://github.com/hasktorch.png","language":"Haskell","funding_links":[],"categories":["Haskell Packages","Haskell","Pytorch elsewhere ｜ Pytorch相关","Pytorch elsewhere"],"sub_categories":["Packages Under Active Development","Other libraries｜其他库:","Other libraries:"],"readme":"# Hasktorch\n\nHasktorch is a library for tensors and neural networks in Haskell.\nIt is an independent open source community project which leverages the core C++ libraries shared by PyTorch.\n\nThis project is in active development, so expect changes to the library API as it evolves.\nWe would like to invite new users to [join our Hasktorch discord space](#contributing) for questions and discussions. [Contributions/PR are encouraged](#contributing).\n\nCurrently we are developing the second major release of Hasktorch (0.2). Note the 1st release, Hasktorch 0.1, on hackage is outdated and _should not_ be used.\n\n## Documentation\n\nThe documentation is divided into several sections:\n\n- [Introductory Videos](#introductory-videos)\n- [Getting Started](#getting-started)\n- [Known Issues](#known-issues)\n- [Contributing](#contributing)\n- [Notes for Library Developers](#notes-for-library-developers)\n\n## Introductory Videos\n\n- [High-level MuniHac talk](https://www.youtube.com/watch?v=Qu6RIO02m1U) by [@austinvhuang][austin-twitter]\n- [Hands-on live-coding demo](https://www.youtube.com/watch?v=ZnYa99QoznE\u0026t=1689) by [@tscholak][torsten-twitter]\n- [Low-level FFI talk](https://www.youtube.com/watch?v=qWpD8t_Aodk\u0026feature=youtu.be) by [@junjihashimoto][junji-twitter]\n\n\n## Getting Started\n\nThe following steps will get you started.\nThey assume the hasktorch repository has just been cloned.\nAfter setup is done, read the [online tutorials](https://hasktorch.github.io/tutorial/) and [API documents](https://hasktorch.github.io/).\n\n* [linux+cabal+cpu](#linuxcabalcpu)\n* [linux+cabal+cuda11](#linuxcabalcuda11)\n* [macos+cabal+cpu](#macoscabalcpu)\n* [linux+stack+cpu](#linuxstackcpu)\n* [macos+stack+cpu](#macosstackcpu)\n* [nixos+cabal+cpu](#nixoscabalcpu)\n* [nixos+cabal+cuda11](#nixoscabalcuda11)\n* [docker+jupyterlab+cuda11](#dockerjupyterlabcuda11)\n\n\n### linux+cabal+cpu\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ pushd deps       # Change to the deps directory and save the current directory.\n$ ./get-deps.sh    # Run the shell script to retrieve the libtorch dependencies.\n$ popd             # Go back to the root directory of the project.\n$ source setenv    # Set the shell environment to reference the shared library locations.\n$ ./setup-cabal.sh # Create a cabal project file\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ cabal build hasktorch  # Build the Hasktorch library.\n$ cabal test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ cabal build examples  # Build the Hasktorch examples.\n$ cabal test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=cpu                      # Set device to CPU for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n\n### linux+cabal+cuda11\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ pushd deps              # Change to the deps directory and save the current directory.\n$ ./get-deps.sh -a cu118  # Run the shell script to retrieve the libtorch dependencies.\n$ popd                    # Go back to the root directory of the project.\n$ source setenv           # Set the shell environment to reference the shared library locations.\n$ ./setup-cabal.sh        # Create a cabal project file\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ cabal build hasktorch  # Build the Hasktorch library.\n$ cabal test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ cabal build examples  # Build the Hasktorch examples.\n$ cabal test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=\"cuda:0\"                 # Set device to CUDA for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n\n### macos+cabal+cpu\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ pushd deps       # Change to the deps directory and save the current directory.\n$ ./get-deps.sh    # Run the shell script to retrieve the libtorch dependencies.\n$ popd             # Go back to the root directory of the project.\n$ source setenv    # Set the shell environment to reference the shared library locations.\n$ ./setup-cabal.sh # Create a cabal project file\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ cabal build hasktorch  # Build the Hasktorch library.\n$ cabal test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ cabal build examples  # Build the Hasktorch examples.\n$ cabal test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=cpu                      # Set device to CPU for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n\n\n### linux+stack+cpu\n\nInstall the Haskell Tool Stack if you haven't already, following [instructions here](https://docs.haskellstack.org/en/stable/README/)\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ pushd deps     # Change to the deps directory and save the current directory.\n$ ./get-deps.sh  # Run the shell script to retrieve the libtorch dependencies.\n$ popd           # Go back to the root directory of the project.\n$ source setenv  # Set the shell environment to reference the shared library locations.\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ stack build hasktorch  # Build the Hasktorch library.\n$ stack test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ stack build examples  # Build the Hasktorch examples.\n$ stack test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=cpu                      # Set device to CPU for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n### macos+stack+cpu\n\nInstall the Haskell Tool Stack if you haven't already, following [instructions here](https://docs.haskellstack.org/en/stable/README/)\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ pushd deps     # Change to the deps directory and save the current directory.\n$ ./get-deps.sh  # Run the shell script to retrieve the libtorch dependencies.\n$ popd           # Go back to the root directory of the project.\n$ source setenv  # Set the shell environment to reference the shared library locations.\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ stack build hasktorch  # Build the Hasktorch library.\n$ stack test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ stack build examples  # Build the Hasktorch examples.\n$ stack test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=cpu                      # Set device to CPU for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n\n\n### nixos+cabal+cpu\n\n(Optional) Install and set up Cachix:\n\n```sh\n$ nix-env -iA cachix -f https://cachix.org/api/v1/install  # (Optional) Install Cachix.\n# (Optional) Use IOHK's cache. See https://input-output-hk.github.io/haskell.nix/tutorials/getting-started/#setting-up-the-binary-cache\n$ cachix use hasktorch                                     # (Optional) Use hasktorch's cache.\n```\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ nix develop  # Enter the nix shell environment for Hasktorch.\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ cabal build hasktorch  # Build the Hasktorch library.\n$ cabal test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ cabal build examples  # Build the Hasktorch examples.\n$ cabal test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=cpu                      # Set device to CPU for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n\n\n### nixos+cabal+cuda11\n\n(Optional) Install and set up Cachix:\n\n```sh\n$ nix-env -iA cachix -f https://cachix.org/api/v1/install  # (Optional) Install Cachix.\n# (Optional) Use IOHK's cache. See https://input-output-hk.github.io/haskell.nix/tutorials/getting-started/#setting-up-the-binary-cache\n$ cachix use hasktorch                                     # (Optional) Use hasktorch's cache.\n```\n\nStarting from the top-level directory of the project, run:\n\n```sh\n$ cat \u003e nix/dev-config.nix\n{\n  profiling = true;\n  cudaSupport = true;\n  cudaMajorVersion = \"11\";\n}\n$ nix develop  # Enter the nix shell environment for Hasktorch.\n```\n\nTo build and test the Hasktorch library, run:\n\n```sh\n$ cabal build hasktorch  # Build the Hasktorch library.\n$ cabal test hasktorch   # Build and run the Hasktorch library test suite.\n```\n\nTo build and test the example executables shipped with hasktorch, run:\n\n```sh\n$ cabal build examples  # Build the Hasktorch examples.\n$ cabal test examples   # Build and run the Hasktorch example test suites.\n```\n\nTo run the MNIST CNN example, run:\n\n```sh\n$ cd examples                            # Change to the examples directory.\n$ ./datasets/download-mnist.sh           # Download the MNIST dataset.\n$ export DEVICE=\"cuda:0\"                 # Set device to CUDA for the MNIST CNN example.\n$ cabal run static-mnist-cnn -- ./mnist/ # Run the MNIST CNN example.\n```\n\n### docker+jupyterlab+cuda11\n\n[This dockerhub repository](https://hub.docker.com/repository/docker/htorch/hasktorch-jupyter) provides the docker-image of jupyterlab.\nIt supports cuda11, cuda10 and cpu only. When you use jupyterlab with hasktorch, type following command, then click a url in a console.\n\n```sh\n$ docker run --gpus all -it --rm -p 8888:8888 htorch/hasktorch-jupyter\nor\n$ docker run --gpus all -it --rm -p 8888:8888 htorch/hasktorch-jupyter:latest-cu11\n```\n\n## Known Issues\n\n### Tensors Cannot Be Moved to CUDA\n\nIn rare cases, you may see errors like\n\n```\ncannot move tensor to \"CUDA:0\"\n```\n\nalthough you have CUDA capable hardware in your machine and\nhave followed the getting-started instructions for CUDA support.\n\nIf that happens, check if `/run/opengl-driver/lib` exists.\nIf not, make sure your CUDA drivers are installed correctly.\n\n\n### Weird Behaviour When Switching from CPU-Only to CUDA-Enabled Nix Shell\n\nIf you have run `cabal` in a CPU-only Hasktorch Nix shell before,\nyou may need to:\n\n* Clean the `dist-newstyle` folder using `cabal clean`.\n* Delete the `.ghc.environment*` file in the Hasktorch root folder.\n\nOtherwise, at best, you will not be able to move tensors to CUDA,\nand, at worst, you will see weird linker errors like\n\n```\ngcc: 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\n`cc' failed in phase `Linker'. (Exit code: 1)\n```\n\n\n## Contributing\n\nWe welcome new contributors.\n\nContact us for access to the [hasktorch discord channel][discord].\nYou can send an email to [hasktorch@gmail.com][email] or on twitter as [@austinvhuang][austin-twitter],\n[@SamStites][sam-twitter], [@tscholak][torsten-twitter], or [@junjihashimoto3][junji-twitter].\n\n[email]:mailto:hasktorch@gmail.com\n[austin-twitter]:https://twitter.com/austinvhuang\n[sam-twitter]:https://twitter.com/samstites\n[torsten-twitter]:https://twitter.com/tscholak\n[junji-twitter]:https://twitter.com/junjihashimoto3\n[slack]:https://hasktorch.slack.com\n[gitter-dh]:https://gitter.im/dataHaskell/Lobby\n[discord]:https://discord.gg/a5Sw5x6RnG\n\n## Notes for library developers\n\nSee [the wiki](https://github.com/hasktorch/hasktorch/wiki) for developer notes.\n\n### Project Folder Structure\n\nBasic functionality:\n\n- `deps/` - submodules and downloads for build dependencies (libtorch, mklml, pytorch) -- you can ignore this if you are on Nix\n- `examples/` - high level example models (xor mlp, typed cnn, etc.)\n- `experimental/` - experimental projects or tips\n- `hasktorch/` - higher level user-facing library, calls into `ffi/`, used by `examples/`\n\nInternals (for contributing developers):\n\n- `codegen/` - code generation, parses `Declarations.yaml` spec from pytorch and produces `ffi/` contents\n- `inline-c/` - submodule to inline-cpp fork used for C++ FFI\n- `libtorch-ffi/`- low level FFI bindings to libtorch\n- `spec/` - specification files used for `codegen/`\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasktorch%2Fhasktorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhasktorch%2Fhasktorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasktorch%2Fhasktorch/lists"}