Numerical contracts: what safe mode checks#
The library’s ambient safety mode (see the user guide’s “Safe and unsafe mode”) checks genuine
numerical preconditions and skips them under safety.unsafe() or a jax trace. This page is the
op-by-op reference: which operations check what, which properties are only caveats, and the
settled answer to “which operations need minimal ranks” (none — a reference result that does not
exist in the literature).
The classification rule#
Precondition — the operation is undefined or numerically wrong without the property. Safe mode checks it and raises; unsafe mode / under-jit skips the check.
Caveat — the operation is valid and correct as computed; the property only governs what the result means (e.g. “this coordinate inner product equals Hilbert–Schmidt”). Never enforced — enforcing it would reject legitimate use.
The classic example: orthogonality/gaugedness is a caveat for a raw coordinate inner product
(it is valid on any frame; the properties only make it equal HS) — which is why the semantic
inner/norm live on the MANIFOLD geometry (where the properties are preconditions) while
T3Tangent.corewise_inner/corewise_norm are the raw coordinate ops (no HS claim, no check).
Equality is explicit#
== is intentionally not defined on the runtime classes (TuckerTensorTrain, weights, frames,
variations, tangents, and their uniform twins): it raises a TypeError directing you to say which
equality you mean (x == x is True by identity; the classes are unhashable – key caches by
id(obj) and keep the object alive). The reason is honesty, not laziness: the arithmetic operators
act on the represented tensors, but exact mathematical equality is not a well-posed floating-point
predicate for an implicit representation (two representations of the same tensor differ in the last
ulp), and a tolerance inside == would not be an equivalence relation. The named checks are:
a.allclose(b, rtol=, atol=)– numerical equality of what the objects represent, per stack element (reduce with.all()), via‖a − b‖ ≤ atol + rtol·max(‖a‖, ‖b‖)with the difference formed in the representation and its norm taken through orthogonalization (stable exactly whena ≈ b). For trains this is gauge- and padding-invariant tensor equality; for frames it is the base point (gauge-invariant) – the same-FRAME / same-tangent-space question issafety.frames_equal; for tangents it checks the same-frame precondition first. Defaultrtolis the ambient jax-aware tolerance (safety.comparison_rtol).a.corewise_equal(b)– bitwise equality of the stored representation (shape, masks, and cores exactly;Falseon any mismatch, never raises).
Value-based hash/eq exists exactly where jit requires it: the aux_data family (mask holders,
geometries, sampling kinds), whose equality decides compiled-program reuse
(backend.common.ExplicitEquality records the rule).
The numerical properties and their checkers#
property |
meaning |
checker |
|---|---|---|
same-frame ( |
two tangents share one tangent space — their frames are numerically equal ( |
built into the checked ops |
orthogonal ( |
the frame is orthonormal (D/U/L/R conditions) |
|
gauged ( |
the variations satisfy the gauge conditions (47)–(48), relative to the tangent’s scale (its operands’ for an arithmetic result — below) |
|
minimal rank |
no stored rank is redundant |
|
tied factors ( |
the Tucker factors are numerically equal within each sharing group ( |
|
Checkers are per stack element (they return a verdict of shape stack_shape); a safe-mode
precondition requires all elements to pass. The checks are cheap in loops: the underlying
residuals (T3Frame.orthogonality_residual, T3Tangent.gauge_residual) are cached on the frozen
objects, so a fixed frame or tangent reused across an inner loop is contracted once. The tolerance
is jax-aware (rtol_jax, looser, when any operand is a jax array — jax defaults to float32 — else
rtol_numpy).
The gauge-check scale: own norm or operand scale#
The gauge residual is relative — the gauge grams D Vᵀ, Lᵀ H over a scale — so a correctly
gauged tangent passes at any magnitude and a tiny ungauged one fails. Which scale matters for one
pattern. A tangent computed directly (a projection, a gradient, a constructor) is measured against
its own coordinate norm. A tangent built by arithmetic is measured against its operands’:
max(‖a‖, ‖b‖) for a ± b, |c|·‖a‖ for c·a (per stack element, recursively;
stack_tangents stacks them), as a floor under its own norm, so it is never stricter than the
self-relative check. That is the yardstick floating point obeys: a − b inherits each operand’s
roundoff gauge violation, ~1e-16·‖a‖, so when a and b agree to 1e-8 — the
finite-difference check of a Hessian — the difference is gauged to 1e-16 of its operands but only
to 1e-8 of itself, and a self-relative check at the 1e-9 tolerance would reject it. A tangent
ungauged in its own right is still caught when it is checked itself, or inside a sum whose operands
are of its own size. The yardstick follows the arithmetic, though: dividing by the step (the
/ (2 * h) below) divides the recorded scale too, so H_fd − Hp is checked only to the tolerance
times ‖grad‖ / (2h) — the finite difference’s own roundoff level, which can dwarf ‖Hp‖. To
certify a computed tangent’s gauge, check that tangent (Hp.is_gauged()), not a difference.
>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> import t3toolbox.manifold as t3m
>>> import t3toolbox.fitting as fitting
>>> import t3toolbox.backend.tv_operations as tvo
>>> M = t3m.MANIFOLD
>>> np.random.seed(0)
>>> shape, ranks = (4, 5, 6), ((2, 2, 2), (1, 2, 2, 1))
>>> ww, pp = [np.random.randn(20, N) for N in shape], [np.random.randn(20, N) for N in shape]
>>> y = t3.TuckerTensorTrain.randn(shape, *ranks).probe_derivatives(ww, pp, 1)
>>> def model_at(x, hessian='gauss_newton'):
... r = [z - yi for z, yi in zip(x.probe_derivatives(ww, pp, 1), y)]
... return fitting.probe_derivatives_model(M, x, ww, pp, 1, r, hessian=hessian)
>>> model = model_at(t3.TuckerTensorTrain.randn(shape, *ranks), hessian='exact')
>>> p = M.randn(model.frame)
>>> p = p / float(M.norm(p))
>>> grad_at = lambda t: M.project_ambient(model.frame, model_at(M.retract(p * t)).gradient.to_t3())
>>> h = 1e-5
>>> H_fd = (grad_at(h) - grad_at(-h)) / (2 * h) # central difference of the Riemannian gradient
>>> Hp = model.hessian(p)
>>> print(bool(M.norm(H_fd - Hp) < 1e-6 * M.norm(Hp))) # safe mode accepts the near-cancelling difference
True
>>> diff = H_fd - Hp
>>> print(bool(tvo.tv_gauge_residual(model.frame.data, diff.variations.data) > 1e-9)) # vs its own tiny norm
True
>>> print(bool(diff.gauge_residual < 1e-13)) # vs its operands'
True
The operand scale is recorded on the result when the arithmetic runs with checks active (safe mode,
eagerly) — a cached value; the tangent classes still hold only their cores — and it is not part of
the jax pytree. A tangent built under safety.unsafe() or a trace, or rebuilt by jax.tree_util (a
jit round-trip), is measured against its own norm: the conservative default. The price is paid in
safe mode only: each ± computes its operands’ coordinate norms (one pass over each operand’s
cores), a few times the cost of the bare addition at small sizes. Raw-.data users pass
the operand scale themselves, tv_gauge_residual(frame, variations, scale=...)
(utv_gauge_residual for the uniform layer).
What each surface checks in safe mode#
— = no numerical precondition (structural checks — shapes, ranks, pair consistency — still apply
always, in both modes).
T3Tangent#
op |
precondition |
caveat (never checked) |
|---|---|---|
|
FRAMES |
|
|
— |
|
|
FRAMES |
|
|
FRAMES |
“= HS iff orthogonal + gauged” (that claim belongs to |
|
— (unary) |
“= HS iff orthogonal + gauged” |
|
— |
realization is gauge-invariant, valid on any frame |
|
— |
bare |
|
— |
|
|
FRAMES (all leaves) |
|
|
— |
|
the checkers ( |
— |
they are the checks; non-enforcing by design |
The geometries (the semantic inner/norm live here)#
op |
precondition |
caveat |
|---|---|---|
|
FRAMES + ORTH + GAUGE (both operands) |
minimal (for strict HS exactness — see the verdict: not needed) |
|
ORTH + GAUGE (unary — no FRAMES) |
minimal |
|
FRAMES |
— (Euclidean by design; no ORTH/GAUGE) |
|
— |
|
|
ORTH |
minimal (for oblique’s HS-matching purpose) |
|
ORTH (gauge-invariant, so ORTH only) |
minimal (strict rank preservation — without it, retract drops numerically-redundant rank) |
|
ORTH |
— |
|
ORTH (the new frame) |
— |
|
ORTH (via the projection) |
minimal (for a true Gaussian on |
|
— |
|
fitting.LocalModel#
op |
precondition |
|---|---|
|
FRAMES — |
|
FRAMES (the same check). |
The exact Hessian (hessian='exact')#
No new enforced precondition: every new error is structural (an unknown hessian / step_control /
g0_reference string; an option where it would have no effect — curvature_damping with Gauss-Newton or
on a corewise geometry, tr_radius0 with the line search, on_line_search_failure='accept' with the trust
region; a non-positive g0norm_*). What the result means carries caveats, never checked:
caveat |
what it says |
|---|---|
exactness vs damping |
The Hessian is exact at |
non-minimal frames |
On every frame |
shared factors |
On a shared manifold the tied lift is exact where the group spectrum |
flat slots |
The curvature term has no component on a slot at maximal rank, and none at all at |
|
A resolution limit, not a convergence certificate: the predicted change of a rejected trust-region step fell below the objective’s floating-point resolution. |
the retraction |
The exact model’s |
The optimizers’ relative stop#
op |
precondition |
caveat (never checked) |
|---|---|---|
|
— (an explicit |
the zero reference is a convention: the gradient norm at the zero tensor of |
|
— (a corewise geometry, a geometry without an |
the cone reference is measured at the zero tensor on the frame aligned with the data gradient there: a fixed point of the loop reached from a hint-free start (in the measurements, a local maximum of the loop’s merit, one of many); |
The padded and aligned frames#
op |
precondition |
contract |
|---|---|---|
|
TIED for |
|
|
the tangent’s frame is a padded frame of the given old ranks (structural) |
one pass; a store not in |
|
ORTH and F represents |
iteration 0’s local model is built at |
Precondition-free surfaces#
surface |
why |
|---|---|
|
exact HS for any cores ( |
|
exact for any cores; only structural checks |
|
the raw coordinate layer; structural shape-match only |
the backend sampling machinery ( |
bare |
|
structural validation always (both modes); the orthogonal representation produces an orthonormal frame |
Minimal ranks: the settled verdict#
Minimal rank splits into structural (has_minimal_ranks — ranks equal the structural minimum;
cheap integer arithmetic) and numerical (has_numerically_minimal_ranks — no stored rank
numerically redundant). Which operations actually need minimal ranks was settled empirically
(each op compared on a minimal vs a non-minimal orthogonal frame against the dense oracle):
op |
needs minimal? |
evidence |
|---|---|---|
|
NO |
the gauged coordinate inner product equals the dense HS value exactly on a non-minimal orthonormal frame — orthogonal + gauged suffices |
|
NO |
the formula matches the true tangent-space dimension for any ranks |
|
caveat only |
on a structurally non-minimal but full-rank frame it is still a valid first-order retraction that drops the redundant rank (desirable cleanup) — minimal buys only strict rank preservation. At a numerically rank-deficient frame (zero singular values: a zero-padded |
|
NO |
correct on any orthonormal frame |
|
NO |
orthogonality suffices |
Verdict: minimal rank is not a correctness precondition for any verified operation, so it is
never checked — a minimal-rank gate would reject legitimate rank-redundant frames for no
correctness reason. It survives as the retract rank-preservation caveat and as the diagnostic
checkers. Naming rule: bare minimal_ranks means the structural notion; numerically_minimal means
the SVD-grade notion.
Why excess rank is inert (the mechanism, worth knowing before you reach for a rank check): a frame
carries four rank stores — down/up and left/right — and an over-ranked request is absorbed as
slack between them rather than breaking any single core’s orthonormality. So an orthonormal frame need
not be minimal: t3_orthogonal_representations does not guarantee it, and neither does
T3Frame.random_orthogonal, which happily returns down_ranks=(4,4,4) with up_ranks=(2,4,2) if you
ask for a Tucker rank the TT bonds cannot support. Such a frame is exactly orthogonal and every tangent
operation on it is exact — the tangent space, its dimension, and the HS identities are unchanged. Full
derivation and the measured numbers: frame_variations.md. Build from a
minimal-rank point only if you need retraction to preserve the rank tuple.
The decision record behind this page — the op-by-op sweep, the experiments, and the sign-off —
is preserved as contributor/numerical_contract_catalog.md.