Skip to main content

torch2casadi

Export a PyTorch model and its derivative graphs to ONNX, then use it in CasADi optimization. PyTorch is needed at export time; evaluation uses CasADi's native ONNX Runtime backend.

An MIT-licensed early-development package.

Source: https://github.com/casadi/torch2casadi

import torch
import casadi as ca
from torch2casadi import export

model = torch.nn.Sequential(
    torch.nn.Linear(2, 16), torch.nn.Tanh(), torch.nn.Linear(16, 2)
).eval()
path = export(model, torch.zeros(1, 2), "generated")
f = ca.GraphBuilder(str(path)).create("f")

opti = ca.Opti()
u = opti.variable(2)
opti.minimize(ca.sumsqr(f(u) - ca.DM([0.9, -0.2])))
opti.subject_to(opti.bounded(-1, u, 1))
opti.solver("sqpmethod", {"qpsol": "qrqp"})
solution = opti.solve()

The trained-surrogate example in examples/opti.py includes a coupled constraint and regularization. It uses exact Hessians.

Installation

Use Python 3.10–3.12:

python -m pip install torch2casadi

For development, clone the repository and use python -m pip install -e '.[dev]'. Install the appropriate PyTorch 2.6 CPU/CUDA wheel first if necessary. The dependency versions in pyproject.toml describe the tested exporter stack; they are deliberately narrow for this initial prototype.

For CasADi integration, build branch onnx-primal-efficiency with WITH_ONNX=ON and WITH_ONNX_RUNTIME=ON. The sibling derivative feature is in commit b5040ffd76. Installing the Python onnxruntime wheel does not enable CasADi's native backend. The exporter itself does not depend on the CasADi Python package.

Export contract

  • One tensor input and one tensor output; float32 or float64 on CPU during export.
  • Tensor shapes are fixed by the example. All elements are differentiable, including any model batch axes. No independence between batch elements is assumed.
  • The module is copied, switched to evaluation mode and frozen. The caller's parameters and training mode are preserved.
  • Inputs and outputs are flattened in PyTorch element order and exposed as CasADi column vectors. Derivative direction counts are dynamic.
  • Modules must support the required PyTorch function transforms and ONNX operations. Data-dependent Python branches, stochastic evaluation, integer/discrete inputs, multiple argument/output trees, and unsupported custom operators are outside this initial contract.
  • Smooth models are appropriate for exact-Hessian optimization. ReLU networks do not acquire smooth Hessians through export.

Default files:

File Inputs Output
f.onnx x: nx × 1 y: ny × 1
adj_f.onnx x, adj_y: ny × nadj adj_x: nx × nadj
fwd_adj_f.onnx x, adj_y, fwd_x: nx × nfwd, fwd_adj_y: ny × (nfwd·nadj) fwd_adj_x: nx × (nfwd·nadj)

export(..., forward=True) also writes fwd_f.onnx. name="net" changes the family to net.onnx, adj_net.onnx, etc.

Combined columns are grouped by outer forward direction, then inner adjoint direction. The mixed graph differentiates both the primal input and the adjoint seed. It computes products through AD without forming a dense Jacobian or Hessian as an intermediate.

Each export checks all graphs with ONNX's checker and executes them with independent (nadj, nfwd) counts (1,1), (2,3), (3,2), (4,5) at perturbed inputs. Mixed and forward derivatives are checked against PyTorch forward AD, independently of the reverse-over-reverse export formulation. Files are staged until all checks pass. Export into a fresh model-family directory, so old derivative files cannot silently survive a changed model. Validation uses relative/absolute tolerances of 2e-5/2e-6 for float32 and 1e-8/1e-9 for float64; ORT fusion can round scalar coefficients even in a double graph. These numerical checks supplement the exporter contract; they cannot prove correctness over arbitrary data-dependent behavior.

Keep sibling files together until derivatives are constructed. Constructed CasADi derivatives embed their model bytes, including when serialized.

Development

Run the exporter tests and build the distributions:

python -m unittest discover -s tests
python -m build

For the additional integration tests, put the feature CasADi build on PYTHONPATH and run TORCH2CASADI_INTEGRATION=1 python -m unittest discover -s tests. Those tests require the native ORT plugin and are explicitly skipped in ordinary package CI until the CasADi feature is released.

The tests cover Tanh, Sigmoid, Softplus, tensor-shaped nonlinear algebra, independent seed counts, Hessians through CasADi and serialization after the ONNX files are deleted.

The implementation uses experimental FX tracing APIs and a PyTorch 2.6 export workaround. Broader PyTorch-version/operator support needs a compatibility test matrix before a stable release. Future arbitrary derivative-family export can build on the same packaging convention; this initial package emits first derivatives and forward-over-adjoint, not all nesting patterns supported by CasADi.

Releases and provenance

The release guide describes TestPyPI/PyPI Trusted Publishing and verification. Releases use OIDC with GitHub build-provenance attestations and PyPI publishing attestations.

Metadata

Release files for torch2casadi 0.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 torch2casadi 0.1
File Size Uploaded
torch2casadi-0.1.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch2casadi 0.1
File Interpreter ABI Platform
torch2casadi-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.3 kB

Release files / torch2casadi-0.1.tar.gz

Download URL torch2casadi-0.1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
aca4c2d93174da2e227b4aa7d53ab5cd379341edf564438837d711c5d2054316
BLAKE2b-256 checksum
How to use checksums
0d5ebfe4bb2942b79860ef768765b4285090a7e87ed03234f5154171cc1b36df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 14, 2026.

Transparency log

Release files / torch2casadi-0.1-py3-none-any.whl

Download URL torch2casadi-0.1-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
500e4cbbde78c9eac5fc5c4d4d8372ad86b6eeeb90992da00292992cd1482c2e
BLAKE2b-256 checksum
How to use checksums
8b52c6581745ca26f8d4a76f63ffdec9018ae04f15c437c84b53686343eb75ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 14, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

This release

0.1 This release

2 release files

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