torchquad
High-performance numerical integration on the GPU with PyTorch.
Built With
Getting Started
This is a brief example of setting up torchquad.
Prerequisites
We recommend using conda, especially if you want to utilize the GPU. It will automatically set up CUDA and the cudatoolkit for you in that case.
Note that torchquad also works on the CPU. However, it is optimized for GPU usage.
- conda, which will take care of all requirements for you. For a detailed list of required packages, please refer to the conda environment file.
Installation
- Get miniconda or similar
- Clone the repo
git clone https://github.com/esa/torchquad.git
- Setup the environment. This will create a conda environment called
torchquad
conda env create -f environment.yml
Alternatively you can use
pip install torchquad
Usage
This is a brief example how torchquad can be used to compute a simple integral. For a more thorough introduction please refer to the example notebook.
The full documentation can be found on readthedocs.
# To avoid copying things to GPU memory,
# ideally allocate everything in torch on the GPU
# and avoid non-torch function calls
import torch
from torchquad import MonteCarlo
# The function we want to integrate, in this example f(x,y) = sin(x) + e^y
def some_function(x):
return torch.sin(x[0]) + torch.exp(x[1])
# Declare an integrator, here we use the simple, stochastic Monte Carlo integration method
mc = MonteCarlo()
# Compute the function integral by sampling 10000 points over domain
integral_value = mc.integrate(some_function,dim=2,N=10000,integration_domain = [[0,1],[-1,1]])