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, 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, state = 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.

Reusing the mesh and batch size

When you integrate a similar integrand many times, for example once per optimizer step of a model, each call can start where the last one ended. Every call returns, beside its result, a state with what is worth reusing: result, state = solver.integrate(...).

  • state.mesh, the optimal mesh (the final mesh with low-error subintervals merged), so the next call needs few splits;
  • state.max_batch, the batch size (the most points per call of the integrand known to fit), and state.max_batch_converged, whether it may still grow.

Pass it to the next call as state=state. The solver keeps nothing between calls: a call reuses exactly what you pass, and no call can change what another one reuses.

import torch
from padaquad import adaptive_quadrature

solver = adaptive_quadrature("uniform", "gk21", device="cpu")
a = torch.tensor([0.0], dtype=torch.float64)
b = torch.tensor([1.0], dtype=torch.float64)

# Training: the first iteration builds a fresh mesh on [a, b]; every later
# one starts from the state of the one before.
result, state = solver.integrate(lambda t: torch.cos(t), mesh_init=a, mesh_final=b)
for scale in (1.1, 1.2):
    result, state = solver.integrate(lambda t, s=scale: torch.cos(s * t), state=state)

# Validation on [0, 0.5]: a bound inside the state's mesh cuts it. state is
# not reassigned, so the training loop continues unchanged.
half, _ = solver.integrate(lambda t: torch.cos(1.2 * t), state=state, mesh_final=b / 2)
print(round(float(result.integral), 4), round(float(half.integral), 4))  # 0.7767 0.4705
To reuse Pass
the mesh and the batch size (same or similar integrand) state=state, and bounds inside its mesh to integrate part of it
only the batch size (another interval or your own mesh) the bounds or your mesh, and max_batch=state.max_batch (then fixed), or state=dataclasses.replace(state, mesh=None) with the bounds
nothing the bounds, or a mesh

Building state.mesh takes a few milliseconds after the integration. A call whose mesh no later call starts from can skip it with build_state_mesh=False: its state.mesh is then None, and a later call given that state keeps the batch size but needs both bounds and starts from a fresh mesh.

A call needs a mesh, a state with a mesh, or both bounds. Bounds inside the mesh cut it; a bound outside it extends it, and the integration refines the new subinterval. Without max_batch or a state, the first call tries a few small batches (test calls on copies of one point, whose values are discarded) and then lets the batch size grow by factors of 10 until the integrand runs out of memory; a state carries what it found to the next call.

More, including several integrals per step, checkpoints and when not to reuse max_batch: Reusing the mesh and batch size.

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.

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 call many times; pass state=state to start a call from an earlier one (see "Reusing the mesh and batch size" above). Every constructor option is keyword-only.
  • Both return a pair result, state. The IntegrationResult holds the integral, its estimated error (a signed sum over subintervals), the mesh, and diagnostics such as converged and the split statistics; the IntegrationState holds the optimal mesh and the batch size for the next call.

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 starts from test calls of 10, 100 and 1,000 points (on copies of one point; their values are discarded) and grows by factors of 10 whenever more points are waiting, until the integrand runs out of memory; then it shrinks and stops growing.

  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 (or into four equal pieces, with split_rule="accelerate").

  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). With enforce_convergence (the default when take_gradient=False), accepted subintervals are judged again in every batch from their stored error estimates, and split if the changed integral makes them fail; a fixed error_integral_reference is used until every subinterval is accepted, then replaced by the integral.

  5. Optimal mesh. After convergence, pairs of neighbouring subintervals are merged into the state's mesh, which a later call starts from when it is passed as state=, 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 2.0.0

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 2.0.0
File Size Uploaded Provenance Actions
padaquad-2.0.0.tar.gz 350.1 kB With provenance Details for padaquad-2.0.0.tar.gz

Release files / padaquad-2.0.0.tar.gz

Download URL padaquad-2.0.0.tar.gz
Size 350.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c12289ce9931011e67ae090261dcaf3084ce5a088e646473f84fcbd5ecb3c426
BLAKE2b-256 checksum
How to use checksums
c6086cc93c5c4d930852ba1ccf4aa27c118a357f658e5e0ab85034938facba9d
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 27, 2026.

Transparency log

Security for padaquad 2.0.0

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-2.0.0.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 27, 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