Tesseract-Torch is a lightweight extension to Tesseract Core that wraps Tesseracts as differentiable PyTorch operations, with full support for reverse-mode and forward-mode automatic differentiation.
Read the docs | Explore the examples | Report an issue | Talk to the community | Contribute
The API of Tesseract-Torch consists of a single function, apply_tesseract(tesseract, inputs), which integrates any Tesseract into PyTorch's autograd graph:
result = apply_tesseract(my_tesseract, {"x": x_tensor})
result["y"].sum().backward() # reverse-mode AD
x_tensor.grad # gradients flow through the TesseractNote
Before proceeding, make sure you have a working installation of Docker and a modern Python installation (Python 3.12+).
Important
For more detailed installation instructions, please refer to the Tesseract Core documentation.
-
Install Tesseract-Torch:
$ pip install tesseract-torch
-
Build an example Tesseract:
$ git clone https://github.com/pasteurlabs/tesseract-torch $ tesseract build tesseract-torch/examples/simple/vectoradd_torch
-
Use it as part of a PyTorch program via
apply_tesseract:import torch from tesseract_core import Tesseract from tesseract_torch import apply_tesseract # Load the Tesseract t = Tesseract.from_image("vectoradd_torch") t.serve() # Run it with PyTorch tensors x = torch.ones(1000, requires_grad=True) y = torch.ones(1000) def vector_sum(x, y): res = apply_tesseract(t, {"a": {"v": x}, "b": {"v": y}}) return res["vector_add"]["result"].sum() loss = vector_sum(x, y) loss.backward() print(x.grad) # gradients via the Tesseract's VJP endpoint # Forward-mode AD is also supported via torch.autograd.forward_ad
Tip
To skip Docker, replace Tesseract.from_image("vectoradd_torch") with Tesseract.from_source("tesseract-torch/examples/simple/vectoradd_torch/tesseract_api.py"). This serves the Tesseract in a subprocess, building a virtual environment from its requirements on first use (which needs uv).
Tip
Now you're ready to jump into our examples for more ways to use Tesseract-Torch.
-
Required endpoints: Using
apply_tesseractwith reverse-mode AD (.backward(),torch.autograd.grad) requires the Tesseract to define avector_jacobian_productendpoint. Forward-mode AD (torch.autograd.forward_ad) requiresjacobian_vector_product. -
Other
torch.functransforms are not supported:apply_tesseractworks with PyTorch's standard autograd API (.backward(),torch.autograd.grad,torch.autograd.forward_ad), but not withtorch.functransforms (torch.func.vjp,torch.func.jvp,torch.func.grad,torch.func.jacrev,torch.func.jacfwd). These transforms create functionalized tensors that cannot be converted to NumPy arrays, which Tesseract endpoints require. Callingapply_tesseractinside atorch.functransform will raise a clear error. -
No higher-order derivatives: a gradient taken with
create_graph=Truecan be used, but differentiating it again raises aRuntimeError, since the Tesseract'svector_jacobian_productis opaque to PyTorch. See Troubleshooting. -
torch.vmapneeds avmap_method: passvmap_method="sequential","expand_dims"or"broadcast_all"toapply_tesseractto batch it withtorch.vmap; without one,torch.vmapraises when a batched tensor reachesapply_tesseract. See Batching withtorch.vmap.
Tesseract-Torch is licensed under the Apache License 2.0 and is free to use, modify, and distribute (under the terms of the license).
Tesseract is a registered trademark of Pasteur Labs, Inc. and may not be used without permission.
