Tangent¶
Source-to-source automatic differentiation for Python.
Tangent takes your Python function and generates a new Python function that computes its gradient — code you can read, print, step through in a debugger, and reason about. No graphs, no tapes hidden inside a C++ runtime, no black boxes.
import tangent
def f(x):
return x ** 3 - 2 * x ** 2 + 3 * x - 1
df = tangent.grad(f, verbose=1) # prints the generated gradient code
print(df(2.0)) # f'(2) = 7.0
# What verbose=1 prints - the gradient is just Python:
def dfdx(x, bf=1.0):
...
bx = 3 * x ** 2 * bf - 4 * x * bf + 3 * bf
return bx
Why Tangent?¶
-
Readable gradients
The derivative of your function is ordinary Python source. Inspect it, profile it, or paste it into a code review.
-
Debuggable
Set a breakpoint inside the backward pass. Print an intermediate adjoint. No other autodiff system lets you do this naturally.
-
One API, six backends
The same
tangent.graddifferentiates code written against NumPy, JAX, TensorFlow, PyTorch, Keras 3, or tinygrad. -
Real Python
Loops (with
break/continue/earlyreturn), conditionals, closures, classes, list building, comprehensions, tuple unpacking, containers in and out. -
Higher-order
Forward and reverse mode, second and third derivatives, Hessian-vector products, forward-over-reverse.
-
√n checkpointing
grad(f, checkpoint=True)trades recomputation for memory: 96.8% peak-tape reduction measured, with bit-identical gradients.
Reject-or-be-correct¶
Tangent's guiding principle: a construct either differentiates correctly in every mode — verified against finite differences by a 79,000+ case test suite — or it is rejected with a clear, actionable error. Never a silently wrong gradient.
At a glance¶
| Install | pip install tangent-ad — see Installation |
| Import | import tangent (the import name is unchanged) |
| Python | 3.9 – 3.13 |
| License | Apache 2.0 |
| Lineage | Created by Google Research (2017); revived, completed, and maintained by @pedronahum |