Skip to main content

padaquad

A PyTorch library for adaptive numerical quadrature — computing $\int_{a}^{b} f(t), dt$ for a known integrand $f$, in parallel batches, with full autograd support.

Adaptive quadrature is the workhorse behind every problem that reduces to "integrate this function and we already know how to evaluate it": computing the action along a known trajectory, the loss along a learned path, an expectation under a base measure, an ODE residual along a candidate solution, or simply $\int_{0}^{\pi}\sin(t),dt$. Classical adaptive-quadrature libraries (QUADPACK, scipy.integrate.quad) handle this well, but they are sequential and not differentiable. PyTorch's torchdiffeq is differentiable but solves a different problem (true ODE integration where the next state depends on the previous), so it must evaluate steps sequentially even when the integrand has no state-coupling.

padaquad fills the gap: it is adaptive quadrature, not ODE solving, and it exploits the lack of state coupling to evaluate many subintervals in parallel on GPU. With full autograd through the integration loop, it is suitable for:

  • training a learnable function $\phi_\theta(t)$ whose loss is a path integral, and back-propagating through that integral;
  • computing $\nabla_\theta \int f_\theta(t),dt$ either by autograd through the integral (option A) or by integrating $\nabla_\theta f$ directly (option B);
  • one-shot definite integrals where you want batched parallel evaluation of a smooth integrand; the Performance page compares it with torchdiffeq, with the measurements and how to reproduce them.

What this is for

  • ✅ Compute $\int_{a}^{b} f(t),dt$ where $f$ is given as a callable.
  • ✅ Compute $\int f(t, \phi_\theta(t)),dt$ for a learnable $\phi_\theta$, and back-propagate through it.
  • ✅ Compute integrals of gradients, expectations, residuals — anything the user constructs as a $t\mapsto \mathbb{R}^D$ callable.
  • ✅ Run on GPU or CPU; the parallel evaluation pays off most when evaluating $f$ is itself non-trivial (a neural net, a PDE solver, etc.).

What this is not for

  • ❌ True ODE integration $\dot y = f(t, y)$ with state coupling — the parallel trick relies on independence between subintervals. For state- coupled problems use torchdiffeq, torchode, or diffrax.
  • ❌ Multi-dimensional adaptive integration (cubature). Use a sparse-grid or Monte-Carlo library.
  • ❌ Long-time symplectic / Hamiltonian integration. Use a dedicated geometric integrator.

Installation

pip install padaquad

Or from source:

git clone https://github.com/khegazy/padaquad.git
cd padaquad
pip install -e .

Runtime dependencies: Python 3.11+, PyTorch, NumPy, SciPy, einops, psutil.

For developers:

pip install -e ".[dev]"
pre-commit install

The dev extras add pytest, ruff, mypy, pre-commit, typos, and torchdiffeq (used by the speed-test benchmark only).

Quick start

import math
import torch
from padaquad import integrate

result = integrate(
    torch.sin,
    mesh_init=torch.tensor([0.0], dtype=torch.float64),
    mesh_final=torch.tensor([math.pi], dtype=torch.float64),
)
print(round(float(result.integral), 6))  # 2.0
print(float(result.integral_error))  # estimated error: signed sum over subintervals
print(result.converged)  # True

The integrand f takes a tensor t of shape [N, T] (a batch of points of the integration variable) and returns a tensor of shape [N, D] (its values there). Because f depends only on t, the solver can evaluate the nodes of many subintervals in one call, on a CPU or a GPU.

Differentiable integration

Every operation of the integration is differentiable, so you can take gradients of the result with respect to anything the integrand depends on. There are two equivalent ways to compute $\nabla_\theta \int f_\theta(t),dt$:

import torch
from padaquad import adaptive_quadrature, steps

theta = torch.tensor(1.7, dtype=torch.float64, requires_grad=True)
mesh_init = torch.tensor([0.0])
mesh_final = torch.tensor([torch.pi])


# (A) Backprop through the integral.
solver = adaptive_quadrature(
    sampling_type=steps.ADAPTIVE_UNIFORM,
    method="gk21",
    atol=1e-8,
    rtol=1e-8,
)
solver.integrate(
    f=lambda t: theta * torch.sin(t),
    mesh_init=mesh_init,
    mesh_final=mesh_final,
    take_gradient=True,  # per-batch backward; accumulates into theta.grad
)
print(theta.grad)  # 2.0


# (B) Integrate the gradient of the integrand directly.
df_dtheta = lambda t: torch.sin(t)  # closed-form derivative
out_b = solver.integrate(f=df_dtheta, mesh_init=mesh_init, mesh_final=mesh_final)
print(out_b.integral)  # also 2.0

The two paths agree to machine precision on smooth integrands; this consistency is verified in tests/test_autodiff_consistency.py.

Memory-bounded gradients. Setting take_gradient=True makes the solver call loss.backward() after each accepted batch instead of holding the full autograd graph until the end. This is essential when the number of integrand evaluations is large enough that the graph would otherwise exceed GPU memory.

Adaptive mesh and warm-starting

Each call returns a result.mesh_optimal — the post-refinement, post-pruning mesh that the integration converged to. This mesh is the ideal starting point for a similar integrand on the next call: when training a model, the integrand changes only slightly between iterations, so re-running adaptive refinement from a coarse random mesh wastes work. The sketch below assumes your own integrand f_theta, bounds and optimizer.

solver = adaptive_quadrature(method="gk21", atol=1e-8, rtol=1e-8)

for epoch in range(N_epochs):
    # same_f=True: from the second iteration on, start from the
    # previous run's optimal mesh and max_batch (they replace any mesh or
    # max_batch passed here) and skip the memory benchmark. The first
    # iteration has nothing stored, so it uses a fresh mesh. The default,
    # same_f=False, never reuses a stored mesh for a different
    # integrand.
    result = solver.integrate(
        f=f_theta,
        mesh_init=mesh_init,
        mesh_final=mesh_final,
        same_f=True,
        take_gradient=True,
    )
    optimizer.step()

Available methods

The library ships thirteen quadrature methods in three families: Gauss-Kronrod (gk7, gk15, gk21, gk31; gk21 is the default), Clenshaw-Curtis (cc5, cc9, cc17, cc33, cc65) and Runge-Kutta (adaptive_heun, fehlberg2, bosh3, dopri5). The methods page gives each one's degree, order and sources, generated from the code. fehlberg2's error estimate ignores curvature and can be far too small (see examples/verification/integral_accuracy.py).

Plus two methods for the variable-sampling solver (adaptive_heun and interpolatory3_variable), whose weights are computed from the actual node positions. The integration loop currently places every subinterval's nodes at the method's standard positions and evaluates fresh nodes in both halves of a split, as the uniform solver does, so interpolatory3_variable always has its middle node at the center, where it is Simpson's rule.

For smooth integrands at moderate-to-high accuracy, prefer gk21 or cc33. The low-node methods (gk7, cc5, cc9) waste fewer evaluations on smooth regions while the mesh is refined around a rough feature. The Runge-Kutta methods are kept as baselines.

A method's degree is the highest polynomial degree it integrates exactly; UNIFORM_METHODS["gk21"].degree gives it, and order is degree + 1.

Check the accuracy yourself

Two scripts in examples/verification/ check the claims above on your machine, and the test suite runs them:

  • polynomial_degree.py measures every method's degree from its stored nodes and weights, and checks that its error estimate has the strength the solver assumes. You can do the same for one method with UNIFORM_METHODS["gk21"].measure_degree().
  • integral_accuracy.py integrates functions with known integrals using every method at several tolerances, and checks each actual error against what the tolerance allows.

Use cases

Path integrals over learned functions. Compute $\int f(t, \phi_\theta(t)),dt$ where $\phi_\theta$ is a neural network parameterizing a path. Backprop through the integral updates $\theta$ to optimize the integrand against any objective.

Physics-informed residual minimization. Solve a differential equation by parameterizing the solution as $y_\theta(t)$, then minimizing $\int |\mathcal{L},y_\theta(t)|^2,dt$ where $\mathcal{L}$ is the differential operator. The collocation residual at every $t$ is just a function of $t$, so it fits the quadrature framework exactly.

Expectation under a base measure. Compute $\int f(t),p(t),dt$ where $p$ is a known density and $f$ is a quantity of interest. The integrand is the product f(t) * p(t).

The library does not bundle these as application APIs — they are simply uses of integrate(f, ...) with the right f.

API reference

The full reference, generated from the docstrings, is on the documentation site, https://khegazy.github.io/padaquad/; help(padaquad.integrate) shows the same text. In short:

  • integrate(f, method, sampling, **options) computes one integral. Each keyword option goes to the solver's integrate call if it is one of that call's arguments (mesh_init, mesh_final, mesh, y0, take_gradient, f_args, max_batch, ...), and to the solver constructor otherwise (atol, rtol, remove_cut, merge_rule, device, ...).
  • adaptive_quadrature(sampling_type, method, **options) builds a solver to reuse across calls, for example to warm-start with same_f=True. Every constructor option is keyword-only.
  • Both return an IntegrationResult: the integral, its estimated error (a signed sum over subintervals), the mesh, and diagnostics such as converged, mesh_optimal and the split statistics.

How it works

  1. Starting mesh. When no mesh is given, $[a, b]$ is cut into a few equal pieces, each split again at random points (N_init_steps sets the size). The points are random because evenly spaced subintervals can line up with the features of a periodic integrand, so that every error estimate misses them. Call torch.manual_seed first for results that repeat exactly.

  2. Batched evaluation. The nodes of a batch of subintervals are stacked into one tensor and the integrand is called once on all of them. Unless max_batch is given, the batch size is set from a short measurement of how much memory one evaluation takes, so that batches fill the device memory until a fraction 1 - total_mem_usage of the total remains free.

  3. Error estimate per subinterval. Each method computes the integral of a subinterval twice: with its own weights and with a lower-degree method on the same nodes. The difference $e$ is the error estimate. The error ratio $|e| / \max(\text{atol}, \text{rtol} \cdot |I|)$, where $I$ is the integral (per output element, $|e_d| / (\text{atol} + \text{rtol} \cdot |I_d|)$ with error_norm="failure_fraction"), decides: below 1 the subinterval is accepted, otherwise it is split at its midpoint.

  4. Refinement. Batches of evaluation and splitting alternate until every subinterval is accepted, or until max_adaptive_splits stops the splitting (then converged is usually False).

  5. Optimal mesh. After convergence, pairs of neighbouring subintervals are merged into mesh_optimal, which same_f=True starts from on the next call, when the merged subinterval is predicted to pass with a margin. By default the prediction interpolates the integrand values already stored for the two neighbours onto the merged subinterval's nodes, so no new evaluation is needed; remove_cut sets the margin and merge_rule the prediction.

Comparisons

Library Adaptive subdivision Differentiable Parallel Best for
scipy.integrate.quad ✓ ✗ ✗ classical one-shot smooth quadrature
torchquad ✗ ✓ ✓ fixed-grid methods and Monte Carlo (including VEGAS), also in several dimensions
torchdiffeq adaptive step size ✓ ✗ true state-coupled ODEs $\dot y = f(t, y)$
padaquad ✓ ✓ ✓ path integrals, learned-integrand integration, physics-informed residual losses

torchquad's README (commit fc6f9cd, 2026-10-28) states that it does not implement adaptive subdivision. For one-shot scalar quadrature with no autograd, scipy.integrate.quad is fine. The library's value is when the integrand is a learnable PyTorch function and many evaluations are needed.

References

Generated from docs/references.bib, which lists only sources that were opened and checked (python docs/_ext/readme_references.py rewrites this list).

Citing

To cite padaquad, use CITATION.cff (GitHub shows it under "Cite this repository").

License

CC-BY-4.0. See LICENSE.md for details.

Metadata

Release files for padaquad 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for padaquad 1.4.1
File Size Uploaded Provenance Actions
padaquad-1.4.1.tar.gz 267.6 kB With provenance Details for padaquad-1.4.1.tar.gz

Release files / padaquad-1.4.1.tar.gz

Download URL padaquad-1.4.1.tar.gz
Size 267.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4a4fd5acd9a5afe54b1506ff0894882d1d814ab7b99de6472fbf7a2863b784b1
BLAKE2b-256 checksum
How to use checksums
0509fecfb15c793e5e91bfc00e0caefe4b0502a205c9e6f074b7cdf8d95c593d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for padaquad 1.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

1 file in this release has been uploaded with provenance metadata.

Attestation for padaquad-1.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page