Numerical Stability Guide¶
Best practices for maintaining numerical precision in hyperbolic operations.
Overview¶
Hyperbolic geometry presents unique numerical challenges due to the exponential growth of the conformal factor near the boundary and the involvement of hyperbolic functions (cosh, sinh, atanh). This guide explains these challenges and provides strategies to maintain numerical stability.
Key Challenges
- Conformal factor explosion: λ(x) grows exponentially as points approach the boundary
- Float32 limitations: ~7 significant digits, not enough for critical operations past scaled radius \(a = \sqrt{c}\,d \approx 10\) from the origin (see the table below)
- Hyperbolic function overflow: cosh/sinh overflow for large arguments
- Division by near-zero: Operations involving 1 - c||x||² near the boundary
These challenges are specific to the Poincaré ball. The hyperboloid is covered below; Klein and the half-space model have their own limits, see Klein and Half-Space.
Each model's pairwise operations hold up to a scaled radius \(a = \sqrt{c}\,d\) (at \(c = 1\)). Past it
the chart cannot represent the point; that is a property of the chart, not a bug. The hyperboloid
has no such ceiling below \(a \approx 45\), where computing its float32 time coordinate overflows;
its float32 error grows with the radius instead (the numbers are the median dist error over
pairs at distance 1).
| Model | Good to \(a\) (float32 / float64) | What sets the limit |
|---|---|---|
| Hyperboloid | float32 error \(\propto \varepsilon\sinh a\): 5e-4 at 12, 6e-2 at 16.6 / much further | the storage floor \(\varepsilon\sinh(a)/\sqrt{c}\) of a stored point, which reaches 1 at 16.6 |
| Poincaré | 12.65 / 27.7 (13.80 / 28.88 at \(c = 0.1\)) | the ball chart's ceiling, see The Round-Trip Ceiling |
| Klein | 6.32 / 13.86 | half the Poincaré radius, and the floor \(\varepsilon\cosh^2(a)\), see Klein |
| HalfSpace | inf/NaN past 88.7 / 709.8 |
a stored point's rounding, which grows with the distance to the vertical axis, see Half-Space |
Float Precision: Float32 vs Float64¶
When to Use Each¶
Float32 (default):
- Sufficient up to scaled radius \(a = \sqrt{c}\,d \approx 7\), and up to \(a \approx 10\) outside critical operations
- Much faster on GPU for large matrix products: on an A100 a 4096 × 4096 matmul runs at 90 to
100 TFLOP/s in float32 at the JAX default (TF32), 17 in full float32 (
"highest") and 15 in float64. Small stacks of hyperbolic layers (width 256, batch 2,048, forward plus backward) take 0.1 to 0.6 ms per step, and there the gap is much smaller: fourHTCLinearorFGGLinearlayers cost 5 to 25 % more in float64, and the ratio varies with the stack (twoHTCLinearlayers: about 1.9×, 0.2 ms against 0.1 ms). Consumer cards run float64 at 1/32–1/64 of their float32 rate (vendor specifications) - Lower memory footprint (important for large models)
- ~7 significant decimal digits
Float64 (high precision):
- Recommended for critical operations from \(a \approx 10\); required past the float32 chart ceiling, \(a \approx 12.65\) at \(c = 1\) (13.80 at \(c = 0.1\))
- Better numerical stability in edge cases
- ~15-16 significant decimal digits
- Use for research, validation, or stability-critical applications
import jax
import jax.numpy as jnp
from hyperbolix.manifolds import Poincare
# Float32 (default)
poincare_f32 = Poincare()
x = jnp.array([0.1, 0.2])
y = jnp.array([0.8, 0.5])
dist = poincare_f32.dist(x, y, c=1.0)
# Float64 (high precision) — inputs are automatically cast
jax.config.update("jax_enable_x64", True) # required for float64
poincare_f64 = Poincare(dtype=jnp.float64)
dist = poincare_f64.dist(x, y, c=1.0) # returns float64
Precision Requirements by Distance¶
| Scaled radius \(a = \sqrt{c}\,d\) | Float32 relative error, median / max | Recommended Precision |
|---|---|---|
| 1 | 6.1e-8 / 2.5e-7 | float32 |
| 3 | 1.2e-7 / 5.1e-7 | float32 |
| 5 | 2.7e-7 / 2.4e-6 | float32 |
| 7 | 5.8e-6 / 1.5e-5 | float32 |
| 10 | 4.6e-5 / 2.2e-4 | float32; float64 for critical ops |
| 12 | 4.8e-4 / 1.1e-3 | float64 for critical ops |
| 12.5 | 8.2e-4 / 2.5e-3 | float64 for critical ops |
| ≥ 12.65 | past the float32 chart ceiling: the ball cannot store the point | float64 required |
Poincaré ball only: float32 against float64 for dist_0, logmap_0, expmap_0 and their
round trip, the worst of the four and of \(c \in \{0.1, 1\}\). The last row is the ceiling at
\(c = 1\) (13.8 at \(c = 0.1\); see The Round-Trip Ceiling). For the
hyperboloid, see The Hyperboloid Origin Chart and the
two-point section below.
Quick Check
Compute the largest scaled radius \(a = \sqrt{c}\,d_0\) of your embeddings with your own \(c\).
From \(a \approx 10\), use float64 for critical operations. A float32 point cannot sit past the
chart ceiling \(2\,\mathrm{atanh}(1 - \sqrt{c}\,\varepsilon^{0.75})\), 12.65 at \(c = 1\) and
13.80 at \(c = 0.1\) (14.33 below \(c \approx 0.035\), where the atanh clip binds first; see
The Round-Trip Ceiling), so a maximum within ~0.1 of it means
the ball has capped points, and float64 is required:
import math
import jax
import jax.numpy as jnp
from hyperbolix.manifolds import Poincare
c = 0.1 # the curvature your model uses
poincare = Poincare()
v_batch = jax.random.normal(jax.random.PRNGKey(0), (256, 16)) # stand-in for your features
x_batch = jax.vmap(lambda v: poincare.expmap_0(v, c))(v_batch) # ... and embeddings
a = jnp.sqrt(c) * jax.vmap(lambda x: poincare.dist_0(x, c))(x_batch) # scaled radius
eps = float(jnp.finfo(jnp.float32).eps)
# proj margin √c·eps**0.75; below c ≈ 0.035 the tanh/atanh clip 10·eps binds first
margin = max(math.sqrt(c) * eps**0.75, 10 * eps)
a_ceiling = 2 * math.atanh(1 - margin) # float32: 12.65 at c = 1, 13.80 at c = 0.1
print(f"max a = {float(a.max()):.2f}, float32 ceiling = {a_ceiling:.2f}")
# max a > 10: create Poincare(dtype=jnp.float64) for critical ops;
# max a within ~0.1 of the ceiling: the ball has capped points, float64 is required
TF32 on Ampere and Hopper GPUs¶
XLA:GPU runs float32 matmuls in TF32 by default. Its 10-bit mantissa has a machine epsilon of ~1e-3 against float32's ~1e-7 and leaves about 3e-4 relative error in a product of zero-mean matrices, so it silently costs three to four of your seven significant digits.
hyperbolix splits its float32 dot products in two:
- Geometry is pinned to
jax.lax.Precision.HIGHESTand is not configurable, with one named exception (LorentzMLA(score_precision=...), for its two score einsums only; the default keeps the pin): the manifold vector dots, the Lorentz and Poincaré midpoints, the conv patch extraction, the attention score and aggregation einsums, the point-to-hyperplane einsums of the MLR heads and of the PLFC, Poincaré++ and proper-velocity linear and conv layers, and the Minkowski productx @ VofFGGLinear/FGGConv2D. These dots cancel, and apart from the FGG product they are not where the throughput is. The FGG pin measured 2 % on the forward pass and 9 % on forward plus backward for an IMPALA-ResNet of FGG layers at batch 1,024 (one H100; single layers 5 to 16 % on forward plus backward). - The other layer weight GEMMs follow JAX:
HTCLinear(the attention Q/K/V projections included), the FHCNN/FHNN linears,HypLinearPoincare,LorentzConv2D's convolution, the VQ codebook matmul. On an Ampere or Hopper card these run in TF32 unless you say otherwise.
To run everything in full float32, set JAX's own knob:
jax.config.update("jax_default_matmul_precision", "highest")
# or scoped to a block (both spellings are jit-cache aware — changing them re-traces):
with jax.default_matmul_precision("highest"):
logits = model(x)
What TF32 costs on one layer was measured on the FGG product before it was pinned: on an A100
(float32 against a float64 reference), the relative error of FGGLinear was 2.6e-4 at TF32
against 6.8e-8 under HIGHEST. HIGHEST replaces one TF32 pass with a three-pass float32
emulation, measured +10 % to +25 % on a jitted HyperbolicFullAttention forward on an A100
(B = 8, N = 256, 4 heads of 64; the two ends come from two sessions on the same card and code),
and +1 to +3 % with centroid_form="gemm", inside the run-to-run spread.
Depth decides whether you need it. At depth 2 the default and HIGHEST train identically; the
float32 gradient error grows with depth (1.5 % at depth 16 for an HTCLinear stack against 2.5e-6
under HIGHEST). For a deep or gradient-sensitive stack of these layers, set the global knob.
Details: hyperbolix.utils.precision.
The size of a product decides whether it runs in TF32. At the JAX default, XLA compiled small
float32 products into a fused loop, which is full precision, and larger ones into a GEMM kernel,
which is TF32. Measured on an A100 (jax 0.9.1) with a bare jnp.einsum("bi,oi->bo", x, w): a
single row was full float32 at every width tried, 5 to 512 (relative error about 1e-7). The switch
to TF32 (about 3e-4) came between 7 and 8 rows for a 5 → 12 product, between 4 and 5 rows for
8 → 8, and between 1 and 2 rows for 9 → 14, 64 → 64 and 512 → 512. With the default, a sample
evaluated alone can therefore differ from the same sample inside a batch by the TF32 error. LorentzMLA at the size its tests use (model width 9, 6 tokens) differed by 3.3e-3
between one call on two sequences and two calls on one sequence each; under "highest" the
difference was at most 7.6e-6, which is float32 rounding (on CPU the two are bitwise equal).
Why the FGG product is pinned. FGGLinear and FGGConv2D used to follow JAX as well. A
float64 replay of a trained network changed that: a Procgen PPO agent (BigFish) with an encoder
of 15 FGGConv2D and one FGGLinear (c = 0.1, trained on an A100 under the JAX default), 72
checkpoints of 2,048 states each. At the JAX default the greedy action changed 120 times, and in
the runs trained without a cap on the feature radius the actor logits were up to 0.14 off
float64 (median 2.8e-4 to 1.0e-3 from 32k training steps on, 8.7e-3 at initialisation). With jax_default_matmul_precision="highest", which in
that network changes only the 16 FGG products, no greedy action changed and the logits were
within 1.03e-4. The relative error entered at the first conv stage (4e-4) and stayed near
1e-3; it did not accumulate with depth. The effect on the return was not measured.
HIGHEST is a no-op on CPU (there is no TF32 path) and for float64 anywhere.
The Hyperboloid's Two-Point Cancellation Failure Mode¶
The table above is for the Poincaré ball. For the hyperboloid, being far from the origin is not the problem by itself; two points far from the origin and close together is. The literal Minkowski inner product \(\langle x, y\rangle_L = -x_0y_0 + \langle x_s, y_s\rangle\) subtracts two terms of size about \(e^{\sqrt{c}\,(d_0(x) + d_0(y))}\) to leave a result of size \(e^{\sqrt{c}\,d(x,y)}\). The digits lost are set by the Gromov-product-like quantity
and once it exceeds \(\ln(1/\epsilon)\) (15.9 for float32, 36.0 for float64) every digit of the result is cancellation noise. Nearby points on a shared geodesic ray, or clustered leaf embeddings in a deep hierarchy, reach it easily.
hyperbolix never forms that product in its two-point primitives. dist, logmap, sqdist and
tangent_norm are cancellation-free under both version slots (VERSION_DEFAULT /
VERSION_SMOOTHENED, see Hyperboloid Distance Versions). The
single-point operations are covered in The Hyperboloid Origin Chart,
and the places that still lose accuracy are listed below.
Known Limitations¶
expmap, ptransp, tangent_proj, tangent_inner, egrad2rgrad and busemann are also
cancellation-free. Gyro addition is not when its result lies much closer to the origin than its
operands: with x at scaled radius 8 and y at 7.5 on the opposite ray, the float32 result (at
scaled radius 0.5) is off by 3 to 22 %, depending on the direction, and by about 2e-10 in float64. That case is what gyro_difference is
for (see Gyro-Difference and GyroBatchNorm Centering at Large Radius). is_in_manifold and is_in_tangent_space compare the
stored time slot with the value the spatial part implies, so an on-sheet point passes a fixed
atol far out (see The atol Convention).
Six places still lose accuracy at large radius:
ptransp's direction below the float32 angular resolution. A transport step shorter than the point-representation flooreps·sinh(a)/√cis lost in the endpoints' storage rounding, and no formula recovers it.- Attention scores through a GEMM. The Lorentzian similarity score behind
HyperbolicFullAttentionforms2/c + 2⟨Q,K⟩_Lfrom a matrix product, and a GEMM cannot be made cancellation-free the way a single pairwisedistcan — see Full Attention's Float32 Score Floor below. - Transport to the origin from a far point. The generic
Hyperboloid.ptransp(v, far_point, origin)still cancels: its radial component has a float32 error of 1.95e-3 at scaled radius 10. The wrapped-normallog_probdoes not use it: it reads the origin-tangent vector offgyro_differenceand never stores a tangent vector at the mean. What limits it is the storage of the two points. Withzwithin O(1) of a mean at scaled radius \(a\), the stored float32 points are themselves rounded at \(\varepsilon\sinh a\), and no formula recovers what that removes. Forzat scaled distance 0.5 from the mean, displaced across the mean's ray, the float32 gradient with respect to the mean, compared with float64 at the unrounded points, is off by about 5e-3 at \(a = 12\), 0.3 at \(a = 16\) and 1 at \(a = 20\) (relative Euclidean error of the gradient in the mean's spatial coordinates, medians over 16 directions,n = 10,c = 1). Up to \(a = 16\) float64 arithmetic on the same float32-stored points is off by as much, so the error comes from the storage; the float32 arithmetic adds about a tenth of it at \(a = 12\) and about half of it from \(a \approx 18\). Withzat scaled radius 1 the ambient (Euclidean) gradient with respect to the mean stays within about 1e-5 of float64 through \(a = 40\); converting it to a Riemannian gradient at the far mean is a two-point quantity again and has no digits left past the storage floor (off by a factor of about 20 at \(a = 20\)). Just below the float32 coordinate limit of 1.84e19 (\(a \approx 45\) atc = 1) one finite wrong output remains: with the mean at \(a = 44\) the float32 value is still right while the ambient gradient is off by a factor of about 45. Past the limit, float32expmap_0gives the mean aninftime coordinate and the result is NaN. A mean stored with a finite time coordinate, cast from float64 for example, still gives the right value up to \(a \approx 87\) (from about 87.2 it is off, by up to 0.3 %, with no error raised), but its gradient is wrong by orders of magnitude, and non-finite from \(a \approx 84.5\). - The MLR score, when the hyperplane itself sits far from the origin. The reference Lorentz
MLR assembles an
O(1)score from parts of size up toe^(a+|ρ|)/4, withρthe scaled hyperplane offset, so a large bias costs as many digits as a large input radius: near a hyperplane the float32 logit is off by about 1e-3 ata + |ρ| ≈ 10.4, and in the worst case measured from 9 — see The MLR Score at a Large Hyperplane Offset below. - Busemann gradients at a far stored point. When the Busemann layers score a stored float32 point, from scaled radius \(a \approx 29.8\) at \(c = 1\) (\(29.4\) at \(c = 0.3\)) the gradients of the scores with \(\langle x_s, \omega\rangle \ge 0\) can be up to about 1 off relative to their largest entry while the scores stay right (measured on CPU; see Tangent Inputs to the HNN++ and Busemann Layers).
- Float32 gradients of
distwithin about 2 of the coordinate limit (measured on CPU). The squared time coordinate overflows float32 at \(a \approx 45\) for \(c = 1\) and at 43.9 for \(c = 0.1\). Shortly below that, the gradient ofdistwith respect to the far point goes wrong while the value is still right. At \(c = 1\) the reverse-mode gradient is off by a relative 0.2 at \(a = 44\) to 44.25 in dimension 2 and by 0.6 to 1 at \(a = 44\) in dimension 64, where it is already off by 0.5 to 4 % at \(a = 43\); the forward-mode derivative is right up to \(a = 44.25\) and off by 0.7 to 1 at 44.5. At \(c = 0.1\) the reverse-mode gradient at \(a = 43\) is right in dimension 2 and off by 3 to 8 % in dimension 8 and by 0.3 to 0.5 in dimension 64, and both modes are off by 0.7 to 1 from 43.5. On an A100 the same gradients agree with float64 to 6e-7 up to the limit. The A100 keeps float32 subnormals, which XLA's CPU backend flushes to zero, so that flushing is the likely cause.
See also Gyro-Difference and GyroBatchNorm Centering at Large Radius and
ProperVelocity dist/logmap Through the Exact Lift below.
Full Attention's Float32 Score Floor¶
HyperbolicFullAttention's scores are (2/c + 2⟨Q,K⟩_L)/τ, and ⟨Q,K⟩_L is a difference of two
Minkowski terms each of size cosh(a_q)·cosh(a_k)/c. A matrix product returns the Gram matrix only
to absolute eps, so no GEMM spelling avoids the cancellation. The absolute error on one score
has the size eps·cosh(a_q)·cosh(a_k)/(c·τ): in float32 (c = 1, τ = 1) 4.8e-3 at a = 6, 0.26
at a = 8, and 2.0 at a = 9. Measured on 12 tokens in 16 dimensions, the median score is off by
about half of that and the worst of the 144 scores by about 1 to 4 times it. The default τ = √out_features divides the error and the score spread
alike, so from a ≈ 8 the error still exceeds the spread softmax is meant to resolve, and the
weights come out wrong while staying finite.
The cheaper first remedy is a HyperboloidGyroRMSNorm in front of the layer, or a smaller c.
score_dtype=jnp.float64 computes the scores as a float64 island: it casts the spatial parts of
the queries and keys, rebuilds both time coordinates from them in float64, and casts the scores
back before the softmax. Its scores are those of a float64 layer fed the same float32 spatial
coordinates; what remains is the rounding of the stored spatial part. Against a float64 layer on
the unrounded points (c = 1, τ = 1, 16 spatial dimensions, 12 tokens, query and key clouds at
scaled radius a whose directions lie within about 8/sinh(a) of each other, worst of three
draws), the largest attention-weight error at a = 4 / 8 / 12 is 7.5e-5 / 0.21 / 1.0 by default
and 9.4e-7 / 3.4e-5 / 1.7e-3 with the island; other draws move these maxima by up to a factor
of 2. The value aggregation still runs in float32: in self-attention the island's output lies
1.2e-4 from the float64 output at a = 8 and 7.7e-3 at a = 12 (geodesic distance), against
6e-4 to 1e-2 (depending on the draw) and 4.2 by default. Up to 1.5.0 the island also cast the
stored float32 time coordinates, which carry their own rounding into the cancelling difference,
and gained at most about 2× over the default (nothing in the attention weights at a = 12).
The value aggregation has its own form, lorentz_midpoint(form=...), set by centroid_form on
HyperbolicFullAttention and LorentzMLA. "variance" (the default of lorentz_midpoint and
HyperbolicFullAttention) is accurate for far-out values but costs O(N·M·D) work and compiles
slowly on an A100. The forward-plus-backward step of LorentzMLA at S = 512 takes about 55 s
against 9 s (jax 0.9.1) and did not finish at S = 2048 in 10 min; a forward-only jit at S = 512
takes about 9 min against 3 s. HyperbolicFullAttention at N = 512 takes 15 s against 5 s
(forward only: 12 s against 2 s). "gemm" (the default of
LorentzMLA, as in HELM) is one GEMM, but in float32 it loses the centroid once the values pass
a ≈ 6, even when the scores are accurate: values at a ≈ 9 that lie on one geodesic ray
(measured at c = 0.5) put the centroid 11.4 off in geodesic distance, against 6.4e-4 for
"variance". bfloat16 activations (eps 2^16 times float32's) make both worse.
LorentzMLA(score_precision=...) sets the precision of its two score einsums only. The default keeps
them at HIGHEST. A lower value (DEFAULT is TF32 on Ampere/Hopper) adds its own rounding to the
cancellation above. The centroid GEMM always stays at HIGHEST.
The measurement is in float32, on one LorentzMLA with the weights of the trained HELM-MiCE 120M
model (Zenodo record 18729608 with its trailing optimizer step undone; the released file gives the
same radii), on 256 Wikipedia rows of 2,048 tokens, on an A100, with c = 1. There the queries and
keys stay below scaled radius a = 3.9 and the values below 2.83. With the score einsums at DEFAULT
(TF32 on Ampere/Hopper), the worst-token output error is 0.035 nats, against 0.017 nats at HIGHEST.
Both include the error of the TF32 Q/K/V and output projections, which follow JAX's default. With
every GEMM at HIGHEST it is 8.7e-4 nats. The softmax weights of the layer are off by up to 3.9e-3 at
DEFAULT against 2.3e-3 at HIGHEST. On random points, the DEFAULT softmax error grows with
radius: 2.6e-4 at a = 3, 3.9e-3 at a = 4 and 0.16 at a = 6 (median over 5 seeds of the per-seed
maximum, tau = sqrt(300)). HIGHEST stays at or below 2.1e-4 through a = 6. Forward plus backward
of the layer at 4 × 2,048 tokens took 0.74× the HIGHEST time (A/A 1.00). Lowering the centroid GEMM
as well was measured at 0.26 nats on the worst token (0.13 to 0.18 at positions 16 and later), which
is why it stays pinned. With bfloat16 inputs the precision setting made no material accuracy
difference (scores and centroid lowered together). So DEFAULT is a reasonable choice while queries
and keys stay below about a = 4. Past that, keep the pin. For full float32 everywhere, set
jax_default_matmul_precision="highest".
The MLR Score at a Large Hyperplane Offset¶
The hyperplane MLR heads (HypRegressionHyperboloid, HypRegressionPV, HypRegressionPoincarePP)
evaluate the reference Lorentz MLR score of Bdeir et al. 2023,
for a hyperplane with normal z and offset r. Writing a = √c·d for the scaled radius of the
input point, ρ = √c·r for the scaled offset, and θ for the angle between the point and the
hyperplane normal, that is
an O(1) number near the hyperplane, assembled from parts of size up to e^(a+|ρ|)/4: the terms
of the dot product, which cancel among themselves, and the offset term. In our measurements the
float32 logit of a point near a hyperplane is off by about eps·e^(a+|ρ|)/4, with eps = 2^-23
(for c = 1 and ‖z‖ = 1, where the logit is a signed distance; multiply by ‖z‖/√c otherwise).
A hyperplane far from the origin costs as many digits as an input far from the origin. The same
expression sits in the PLFC / ILNN layers, the Poincaré++ MLR (where ρ = 2√c·r), and
FGGLinear's spacelike-V GEMM; the numbers below are measured on the hyperboloid head only.
| Float32 logit error | 1e-4 | 1e-3 | 1e-2 | 0.1 |
|---|---|---|---|---|
| reached at \(a + \lvert\rho\rvert \approx\) | 8.1 | 10.4 | 12.7 | 15.0 |
Measured on the hyperboloid head (CPU, c = 1, 15 classes with unit normals, 32 and 512
dimensions, |ρ| ≤ a), the largest error was 0.6–4 times this law while the law's value is below
about 2; a factor 4 is 1.4 units of a + |ρ|. Past that the asinh compresses the error, which
then grows only logarithmically. Largest logit error for inputs placed exactly on a class
hyperplane, by the largest offset ρ in the head (32 dimensions; draws vary by up to 1.6×):
a \ ρ |
0 | 0.2 | 1 | 2 | 4 | 8 |
|---|---|---|---|---|---|---|
| 8 | 1e-4 | 1e-4 | 5e-4 | 1e-3 | 1e-2 | 0.25 |
| 10 | 5e-4 | 9e-4 | 3e-3 | 1e-2 | 7e-2 | 2 |
| 12 | 4e-3 | 7e-3 | 3e-2 | 8e-2 | 0.5 | 3.9 |
At ρ = 0 an error remains (the first column). Only part of it is beyond the head's reach: at
a = 10 the float32 rounding of the stored input alone moves the exact logit by 2e-4 at 32
dimensions and by 5e-5 at 512, against a head error of 5e-4. The offset lifts the error further,
roughly in proportion to e^|ρ| (measured 1–7 times e^|ρ|).
The gradient follows the logit. Off the hyperplane, the relative error of that class's logit
gradient with respect to the input is about |tanh δ| times the logit error, with δ the scaled
signed distance from the point to the hyperplane (measured within 2 % for a ≤ 12 and offsets up
to 2, and within a factor 0.7–1.25 up to a = 14 and offset 4). Largest relative gradient error
for inputs within a scaled distance of 2 of their hyperplane (32 dimensions, ranges over five
seeds; up to 1.5× larger at 512 dimensions):
| largest offset | a = 8 |
10 | 12 |
|---|---|---|---|
| 0.2 | 4.9e-5–7.2e-5 | 3.8e-4–4.4e-4 | 2.5e-3–3.3e-3 |
| 2 | 5.4e-4–8.0e-4 | 3.4e-3–6.6e-3 | 3.1e-2–4.6e-2 |
Exactly on the hyperplane the first-order term vanishes and the gradient error is far smaller (at
most 1.1e-4 at a = 10 and 7e-3 at a = 12 for offsets up to 2), so a measurement taken only there
understates it.
It takes a point close to a hyperplane. Far from all hyperplanes the asinh compresses the error:
for inputs in random directions (32 and 512 dimensions, 15 classes), 99.9 % of all logits were
within 1.1e-4 at every radius up to a = 14 and every offset up to 8, with rare largest errors of
9e-3 at a = 12 and 4e-2 at a = 14. With normalized features (a ≲ 8) and offsets up to 1 the
logit error stays below 1e-3; at offset 2 it reaches 2e-3–3e-3.
Neither trained head measured so far shows the error. A Lorentz ResNet-18 on CIFAR-100 (HCNN
reference code, three runs) has offsets of 0.4–0.6 with inputs up to a = 5.7. A Procgen
actor-critic agent trained without a radius cap (three seeds, c = 0.1) has offsets of at most
0.15 with inputs up to a = 10, so its a + |ρ| reaches 10.1; its largest actor-logit error was
4.6e-5. Recomputing each head at higher precision on its stored inputs changed none of the
3 × 10,000 test predictions and none of the 36,864 greedy actions.
Remedies: keep a + |ρ| under the value for the accuracy you need, by bounding the trunk's radius
and weight-decaying the head's bias, or run the head in float64. For that, enable x64 and build the
head on Hyperboloid(dtype=jnp.float64): the head computes in its manifold's dtype, and without
x64 a float64 manifold computes in float32 with only a warning. Also rebuild the time coordinate in
float64 from the spatial part, x_0 = √(1/c + ‖x_s‖²), because the offset multiplies the rounding
of a stored float32 time coordinate by sinh|ρ|. At a = 10, ρ = 2 the largest error against the
unrounded input is 8e-3–1.6e-2 in float32, 2e-3–2.6e-3 with a float64 head on the stored point,
and 2e-4 (32 dimensions) or 5e-5 (512) with the time coordinate rebuilt, which is the rounding of
the spatial coordinates themselves. With float64 inputs as well, the law puts a logit error of 1e-3
at a + |ρ| ≈ 30.5; measured there it was 2e-3–6e-3. A cancellation-free half-angle form was
measured at 1.8–2.5× the cost (forward plus backward, on an H100) and not adopted.
FGGLinear's spacelike V has a related eps floor on its column norm, which matters only for a
small weight column with a large bias.
Input Overflow¶
A hyperboloid point whose spatial part passes float32's 1.84e19 has an inf time coordinate
(scaled radius a ≈ 45 at c = 1; see Norms for where this ceiling comes from).
HypLinearHyperboloidPLFC, HypConv2DHyperboloidILNN and HypLinearHyperboloidBusemann pass the
resulting non-finite score through their sinh lift, so the loss is NaN at the minibatch where it
happens. In 1.3.0 and earlier the lift clipped it to a finite, saturated point: the loss stayed
finite and the next kernel and bias gradient was 100 % NaN.
hyperbolix adds no input-side bound here, so as not to hide the divergence. Monitor dist_0 (or
the time coordinate) at the layer's input and bound the trunk that feeds it in your model.
Where Float32 Overflow Still Collapses Silently¶
The library lets non-finite values propagate so that a diverging model fails loudly. At the sites
below, a float32 sum of squares or a clip still turns an absurdly large finite input (or, for the
Poincaré lifts, an inf score) into a finite point. Each needs an input at or near the float32
coordinate ceiling √FLT_MAX ≈ 1.84e19 (see Norms), which only an already-diverging
model reaches, so the code is left as it is.
- Poincaré and κ-stereographic
proj: a finitexwith‖x‖ > 1.84e19becomes the zero vector. Poincare.expmap,Klein.expmap:‖v‖ > 1.84e19returns the base point.Hyperboloid.expmap_0:‖v‖ > 1.84e19returns the origin; a shortervwhose image passes the ceiling gets aninftime slot, as intended.HyperPPFeatureScaling: the mean of squares in its FlaxRMSNormoverflows for a row with‖x‖ > 1.84e19; the row becomes zero, i.e. the origin afterexpmap_0.HRCBatchNorm(train mode): a feature whose batch sum of squares overflows equals its BatchNorm bias in every row, and its running variance becomesinffor good, so eval mode does the same. This starts below the ceiling, at1.84e19/√NforNrows near one value (1.15e18 atN = 256).- HRC (
HRCLayerNorm,HRCRMSNorm, …): reads only the spatial part, so theinftime slot of a point past the ceiling is dropped; the LayerNorm/RMSNorm mean of squares then overflows (‖x_s‖ > 1.84e19) and the row becomes the LayerNorm bias or the origin. lorentz_residual(w_y ≤ 1): the4h²/cin its normalizer,h = sinh(√c·d(x, y)/2), overflows onceh > √(min(c, 1)·FLT_MAX)/2(9.2e18 atc = 1), and the output is the origin — e.g. two points at spatial radius 1.5e19, 120° apart.lorentz_midpointwithc > 1: its normalizer, aboutc·x₀²times the weighted variance of the directionsx_s/x₀(at most 1), overflows in a widely spread cloud from time coordinates of about1.84e19/√c, and the output is the origin. Forc ≤ 1that is past the ceiling.- FHCNN
normalize=True: a linear output whose spatial norm exceeds 1.84e19 gets spatial part 0 and a finite time slot — a finite point off the hyperboloid; its spatial part has zero gradient (the time slot's sigmoid gate still gets one). HypLinearPoincarePP: aninfscore is clipped by thesinhargument clip at±0.99·ln FLT_MAX ≈ ±87.8to a finite point at the ball's edge, where the hyperboloid lift above passes it through. (HypLinearPoincareBusemann'sv_maxclip passes aninflogit through as a NaN row.)HypConv2DPoincare: the samesinhclip; aninfscore comes out as a finite tangent vector along its channel with√c‖out‖ = 43.9, half the clip.
The Hyperboloid Origin Chart¶
The time coordinate \(x_0 = \cosh(\sqrt{c}\,d)/\sqrt{c} \approx (1 + c\,d^2/2)/\sqrt{c}\) cannot
resolve a small radius. dist_0 and logmap_0 therefore read the radius off the spatial
part:
\(\lVert \log_0(y)\rVert = d_0(y)\) holds by construction at every radius, with float32 median
relative error ≤ 1e-7 from radius 1e-6 to 40 (\(c = 1\)). The pairwise dist reads its radial gap
off the spatial part too, so float32 dist(origin, x) agrees with dist_0(x) to 3.1e-7 at every
radius from 1e-8 up; dist_0 stays marginally cheaper and tighter. An inf spatial entry gives an
infinite tangent vector from logmap_0, not NaN.
Norms: One Reduction, Gradient-Safe at Zero¶
The per-sample Euclidean norms in hyperbolix are either safe_sqrt(sum(v**2)), a single pass over
the data, or the max-scaled two-pass safe_norm. Both return an exact 0 with an exactly zero
VJP at the zero vector, where jnp.linalg.norm's VJP is NaN, and both pass a non-finite input
through as inf rather than NaN.
The single reduction overflows once a coordinate passes \(\sqrt{\texttt{finfo.max}}\)
(\(1.84\times10^{19}\) in float32). On the hyperboloid that is scaled radius \(a \approx 45\)
(float32) / \(356\) (float64) for proj and dist_0; logmap_0 uses the two-pass norm and reaches
\(\approx 89\) / \(710\). Typical embeddings sit far inside these limits. Past the ceiling
(float32, \(c = 1\)) the single-reduction sites return:
| Operation | Past coordinate \(1.84\times10^{19}\) |
|---|---|
Hyperboloid.proj, proj_batch |
\(x_0 = \infty\), spatial part unchanged, no NaN |
Hyperboloid.dist_0 (both version slots) |
\(\infty\) |
spatial_to_hyperboloid (HTCLinear), FHCNN linear forward |
\(x_0 = \infty\), no NaN |
| FGG linear forward | non-finite (NaN or \(\infty\), depending on the input) |
Poincaré and \(\kappa\)-stereographic proj (the boundary clamp) |
the origin, a finite point with a finite (exactly zero) gradient |
Poincare.expmap |
the base point, a finite point (the origin, when the base is the origin) |
At the small end, the float32 square of a coordinate below \(1.1\times10^{-19}\) (the square root of
the smallest normal float32) is flushed to zero (measured on CPU), so for a point whose coordinates
all lie below it, Hyperboloid.dist_0, Poincaré dist_0, and Poincaré and Klein dist to the
origin return 0 with a zero gradient (Klein dist is already off when only some coordinates lie below
it). ProperVelocity.dist_0 and Hyperboloid.logmap_0 use the two-pass norm and are right there.
Where a norm is a divisor, floor it multiplicatively around the square root,
floor_at(safe_sqrt(sum(x**2)), MIN_NORM), not under it: under the sqrt, the untaken branch of
the surrounding where still has sqrt'(0) = inf, and \(0 \times \infty\) is NaN. When the floored
norm feeds a \(\sinh(t)/t\), floor \(t\) itself, not only the denominator, or the ratio is \(0\) at
\(t = 0\) instead of \(1\).
In your own code (all primitives are in hyperbolix.utils.math_utils), use safe_norm where you
need the full float32 exponent range, and safe_sqrt(sum(v**2)) on hot per-sample paths.
Storage vs. Compute Dtype¶
- Compute precision is the dtype manifold operations run in, set by the
manifold's
dtype(e.g.Poincare(dtype=jnp.float64)). Manifold methods cast their array arguments and the curvaturecto it on entry, so the output dtype is the manifold's whatever the dtype of the points or ofc. - Storage dtype is the dtype of a layer's parameters and persistent state
(batch-norm statistics, VQ codebooks), set by the
param_dtypeargument on every NN layer (defaultjnp.float32, as in Flax).
A float32 parameter entering a float64 manifold operation is promoted for that computation only. The Riemannian optimizers run in the manifold dtype and cast updates and momentum buffers back to the parameter's dtype.
The recommended high-precision recipe is float64 compute with float32 storage: full precision where the geometry needs it, at half the parameter/optimizer-state memory:
import jax.numpy as jnp
from flax import nnx
from hyperbolix.manifolds import Poincare
from hyperbolix.nn_layers import HypLinearPoincarePP
# Requires global x64 (JAX_ENABLE_X64=1) for the float64 compute path.
manifold = Poincare(dtype=jnp.float64) # compute: float64
layer = HypLinearPoincarePP(manifold, 64, 32, rngs=nnx.Rngs(0)) # storage: float32 (default)
# Fully-float64 networks are an explicit opt-in:
layer_f64 = HypLinearPoincarePP(manifold, 64, 32, rngs=nnx.Rngs(0), param_dtype=jnp.float64)
Parameter dtype rarely matters
Precision in hyperbolic networks is lost in the manifold operations, not
in weight storage. Use param_dtype=jnp.float64 only for reproducibility
studies or numerical debugging.
The Conformal Factor Problem¶
The conformal factor of the Poincaré ball, \(\lambda(x) = 2/(1 - c\lVert x\rVert^2)\), scales tangent vectors in the exponential and logarithmic maps and converts Euclidean gradients to Riemannian ones.
Exponential Growth¶
As points move toward the boundary (\(\lVert x\rVert \to 1/\sqrt{c}\)), λ(x) explodes. A point at scaled radius \(a = \sqrt{c}\,d_0\) has \(\sqrt{c}\,\lVert x\rVert = \tanh(a/2)\), so \(\lambda(x) = 2\cosh^2(a/2) = 1 + \cosh a\), which grows like \(e^a/2\):
import jax.numpy as jnp
from hyperbolix.manifolds import Poincare
poincare = Poincare()
c = 1.0 # at c = 1 the scaled radius a is the distance from the origin
for a in [0, 2, 4, 6, 8, 10, 12, 14, 20]:
# expmap_0(v) lands at distance 2‖v‖ from the origin, so ask for ‖v‖ = a/2
x = poincare.expmap_0(jnp.array([a / 2, 0.0]), c=c)
norm = jnp.linalg.norm(x)
lambda_x = 2.0 / (1.0 - c * norm**2)
print(f"a={a:2d}: ||x||={norm:.6f}, λ(x)={lambda_x:9.1f}, dist_0(x)={poincare.dist_0(x, c):.4f}")
Output:
a= 0: ||x||=0.000000, λ(x)= 2.0, dist_0(x)=0.0000
a= 2: ||x||=0.761594, λ(x)= 4.8, dist_0(x)=2.0000
a= 4: ||x||=0.964028, λ(x)= 28.3, dist_0(x)=4.0000
a= 6: ||x||=0.995055, λ(x)= 202.7, dist_0(x)=6.0000
a= 8: ||x||=0.999329, λ(x)= 1491.6, dist_0(x)=8.0000
a=10: ||x||=0.999909, λ(x)= 11008.7, dist_0(x)=9.9995
a=12: ||x||=0.999988, λ(x)= 81442.8, dist_0(x)=12.0008
a=14: ||x||=0.999994, λ(x)= 155344.6, dist_0(x)=12.6465
a=20: ||x||=0.999994, λ(x)= 155344.6, dist_0(x)=12.6465
Each step of 2 in \(a\) multiplies λ by about \(e^2 \approx 7.4\). Past the float32 chart ceiling,
\(a \approx 12.65\) at \(c = 1\), expmap_0 caps the point, so \(a = 14\) and \(a = 20\) give the same
point.
Pick the dtype from the scaled radius (table above); for
float64, enable jax_enable_x64 and use Poincare(dtype=jnp.float64). addition, expmap and
the layers already project their outputs.
Near the origin, float32 accuracy depends on the backend's transcendental kernels, which are a few
ulps off; hyperbolix's own tanh/atanh wrappers (float32: odd series below 1/8, expm1/log1p
forms above) make the CPU backend match the GPU there. In the tail the float32 derivative of
math_utils.tanh is off by up to 9e-3 (relative) for \(\lvert x\rvert\) in \([4, 6)\) and up to
9e-2 in \([6, 7.15)\), just below the output clamp. That is the same order as jnp.tanh's (1.2e-2
and 0.15 there); it is left as it is, because the fix would be a custom derivative inside a
transcendental.
The Round-Trip Ceiling¶
proj keeps points inside \(1/\sqrt{c}\) by an absolute margin of eps**0.75
(_gyrovector_core._max_norm). A stored point therefore has
\(\sqrt{c}\,\lVert x\rVert \le 1 - \sqrt{c}\,\varepsilon^{0.75}\), and the ceiling in scaled radius
is
| \(c\) | float32 | float64 |
|---|---|---|
| 1 | 12.65 | 27.73 |
| 0.1 | 13.80 | 28.88 |
| 100 | 10.35 | 25.42 |
Since the margin is small, \(a_{\max}(c) \approx a_{\max}(1) - \ln\sqrt{c}\): a larger curvature lowers the ceiling in scaled radius, by \(\ln 10 = 2.30\) at \(c = 100\).
Past the ceiling expmap_0 saturates, and logmap_0(expmap_0(v)) returns the ceiling instead of
v. In float32 below \(c \approx 0.035\) the tanh/atanh wrappers' clip at \(1 - 10\varepsilon\)
binds first, and expmap_0, logmap_0 and dist_0 stop at \(a \approx 14.33\).
The margin caps the conformal factor near \(1/(\sqrt{c}\,\varepsilon^{0.75})\): 1.6e5 in float32 and
5.5e11 in float64 at \(c = 1\). If your embeddings need larger radii, switch to the hyperboloid or
to ProperVelocity rather than shrinking the margin.
Pairwise Operations Near the Boundary¶
The Poincaré pairwise operations are cancellation-free up to the chart ceiling; past it the ball cannot store the point.
-
Distance.
distslots 0 and 2 (the default and the metric-tensor slot) evaluate, with \(B_x = 1 - c\lVert x\rVert^2\),\[ d(x, y) = \frac{2}{\sqrt{c}}\operatorname{arcsinh}\!\left(\frac{\sqrt{c}\,\lVert x - y\rVert}{\sqrt{B_x B_y}}\right), \]which has no domain to clip. Two float32 points on opposite sides at scaled radius 7.2 each (true \(\sqrt{c}\,d = 14.4\), \(c = 1\)) give 14.400112. Slot 1 (
VERSION_MOBIUS) forms \((-x) \oplus y\), a point at the full pair distance, and saturates at the ceiling: 12.637 for a true 14.4, with gradient relative error 1.0. - Möbius denominator.logmap,additionandgyrdivide by \(1 + 2sc\langle x, y\rangle + c^2\lVert x\rVert^2\lVert y\rVert^2\)._mobius_denominatorrewrites \(1 + t^2\) as \((1 - t)^2 + 2t\) with \(t = c\,r_x r_y\), so the subtraction \(1 - t\) happens before the square.logmaptakes its length from the samearcsinhargument asdist._mobius_denominatorfloors both norms atMIN_NORM\(= 10^{-15}\), so for an operand whose norm lies strictly between 0 and 1e-15 (an exact zero is fine) the values stay right but the Jacobians do not: that ofadditionwith respect to such an operand is off by a relative error that grows with the operand's norm, from about 5e-2 at norm 1e-16 to order 1 just below 1e-15, and that ofexpmapwith respect to a tangent of norm 1e-20 at a base point at scaled radius 8 by 4 to 8 (relative; dimensions 8 and 2). Second derivatives are wrong at the exact zero as well, where the first derivative is right: the Hessian ofadditionin an operand that is exactly zero is wrong by up to order \(c\lVert x\rVert/\)MIN_NORM(6e14 in one direction where the true entry is \(-0.98\), at \(\lVert x\rVert = 0.5\), \(c = 1\), both dtypes), andexpmapinherits this at a zero tangent. In float32 the Hessian ofexpmapin a small tangent is unreliable in general: NaN for norms of 1e-15 to 1e-13, wrong by orders of magnitude at 1e-10, off by up to order 1 at 1e-6 (0.003 to 1.4, depending on direction and model) and by up to a few 1e-4 at 1e-3 (float64 at norm 1e-10: 2e-6 on the Poincaré ball, 5e-5 for Stereographic). The float32 Hessian of Stereographiclogmapin its base point is NaN at coincident points. - Transport.ptranspuses the gyration regrouped in \(s = x + y\) (\(= y - x\) for the transport), where each coefficient is \(O(1 - c\lVert x\rVert^2)\) on its own;ptransp(v, x, x)returnsvexactly. In dimension 1, where the gyration is exactly the identity,gyrationof two operands on opposite sides near the chart ceiling is off by about 1e-6 to 1e-5 (relative, float64, scaled radii near 27 and 26, \(c = 0.1\)), far above the inputs' rounding. -apollonian_distwrites its \(G^2\) as \(B_x B_y + c\lVert x - y\rVert^2\), a sum of two non-negative terms.
Gradients at Capped Points¶
Divisors built from \(B = 1 - c\lVert x\rVert^2\) are floored at half the value proj leaves at the
cap (_boundary_divisor_floor), which no projected point reaches, so the floor does not zero the
gradients of capped points. Some float32 gradients at capped points stay wrong, with relative error
up to 1.0 (1.3 for Klein addition): Poincaré expmap with respect to its base point, Poincaré addition with respect to
\(y\), Klein expmap with respect to its base point, and Klein addition on pairs of one capped and
one free point. The floor is not the cause.
Below the cap, the Jacobian of Poincaré expmap with respect to the tangent carries a relative
error of the order of \(\varepsilon\lambda_x\), with \(\lambda_x = 2/(1 - c\lVert x\rVert^2)\) the conformal
factor at the base point: about 1e-4 at scaled radius 8, 1e-3 at 10 and 5e-3 at 12 in float32 (for
steps whose result stays below the cap), and 5e-8 at 20 and 2e-5 at 26 in float64.
Tangent Inputs to the HNN++ and Busemann Layers¶
HypRegressionPoincarePP and HypLinearPoincarePP with input_space="tangent",
HypConv2DPoincare (tangent in, tangent out), and the four Busemann layers with
input_space="tangent" score the tangent vector in closed form and never form the ball point.
For the Poincaré layers, with \(t = \sqrt{c}\,\lVert v\rVert\), the scores use
\(\lambda_x\sqrt{c}\,\langle x, \hat{z}\rangle = \sinh(2t)\,\langle v, \hat{z}\rangle/\lVert v\rVert\)
and \(\lambda_x - 1 = \cosh(2t)\), and HypConv2DPoincare maps its output back with logmap_0 of
the lift in closed form.
These layers are therefore not bound by the ball's ceiling,
\(t = \mathrm{atanh}(1 - \sqrt{c}\,\varepsilon^{0.75})\) ≈ 6.32 at \(c = 1\): past it the scores keep
growing, and the conv output's norm is not capped. At \(t = 8\) the float32 outputs match
float64 to about 1e-5 or better for HypRegressionPoincarePP (9e-7 to 1.2e-5, depending on the
draw) and to below 5e-7 for HypConv2DPoincare and HypRegressionPoincareBusemann (max-abs
error over max-abs value). HypLinearPoincarePP returns a ball point, so its output still stops
at the chart ceiling: at \(t = 8\) its float32 output sits at scaled radius 12.66 and its float64
output at 27.73.
The Hyperboloid Busemann layers place \(v = (0, v_s)\) at scaled radius \(t = \sqrt{c}\,\lVert v_s\rVert\) and score \(\sqrt{c}\,B^\omega = \log(\cosh t - \sinh t\,\langle\omega, \hat v\rangle)\). For random \(\omega\) their float32 outputs and gradients stay within 1.2e-6 of float64 from \(t = 1\) to \(t = 50\). One limit remains on every route: for \(\omega\) within float32 rounding of \(\hat v\), the score depends on the misalignment of \(\omega\) and \(\hat v\) times \(\sinh t\), which float32 cannot resolve at large \(t\) (at \(t = 20\), \(\sqrt{c}\,B\) is off by 0.1 to 3, median 0.8, over 32 draws of \(\omega\) rounded from \(\hat v\)).
The Busemann layers check the manifold at construction and raise a TypeError for a wrong one:
HypRegressionPoincareBusemann and HypLinearPoincareBusemann accept only a Poincare,
HypRegressionHyperboloidBusemann and HypLinearHyperboloidBusemann only a Hyperboloid.
PoincareBatchNorm2D's Batch Mean at Large Radius¶
PoincareBatchNorm2D averages the batch in Klein coordinates, \(\sum\lambda x/\sum(\lambda - 1)\),
and projects that average with proj, so the batch mean cannot sit farther out than the Klein
chart's ceiling: 6.32 at \(c = 1\) and 6.90 at \(c = 0.1\) in float32 (13.86 and 14.44 in float64).
For a cluster at scaled radius 7 (\(c = 1\)), the float32 batch mean reads 6.31–6.33 and lies 0.68
from the float64 one. In float32, keep this layer's inputs inside scaled radius ≈ 6, or run it in
float64.
Init Scale vs. Depth¶
Weight-init failures on hyperbolic layers come in two flavors, and only one of
them is loud. A too-large init pushes outputs toward the Poincaré
boundary or far up the hyperboloid, and you see NaN within a few steps. A too-small init fails silently:
for a linear-in-the-matmul layer (e.g. HTCLinear, whose htc tail applies no
nonlinearity), the per-layer input-Jacobian gain is
and a stack compounds it as \(g^{\text{depth}}\). When \(g < 1\), the input variation the outputs
carry shrinks geometrically until float32 rounds it away. The stack is then a constant map:
gradients are ≈0 from step 0 and training never starts, with no NaN or warning to point at. This
freeze was observed: HTCLinear with init_bound=0.02 froze stacks of depth ≥ 2. The distance is not the cause
(Hyperboloid.dist resolves a spatial gap of 1e-6 in float32). A likely cause, not yet measured:
htc multiplies the full point, time coordinate included, so every output carries an \(O(1)\)
offset from \(x_0 \approx 1/\sqrt{c}\), and variation smaller than \(\varepsilon\) times that offset is
rounded away.
Fixed bounds cannot be width-independent
A hard-coded init bound bakes in a width: U(-0.02, 0.02) has
\(g \approx 0.09\) at fan-in 65 (frozen by depth 2) but \(g \approx 1\) only
near fan-in 7,500. Hold \(g \approx 1\) instead by scaling with fan-in —
e.g. HTCLinear's default init_bound = sqrt(3 / in_features). Layers
followed by normalization (LayerNorm/BatchNorm absorb magnitude) tolerate
\(g > 1\); unnormalized stacks — typical in RL — do not.
See the initialization scales table for each layer family's default and how to recover reference inits.
Flattening a Conv Feature Map: Use LogCat, Not reshape¶
At the conv → FC boundary of a hyperboloid CNN, an (B, H', W', C) feature map (one point
per pixel) must become one point per sample. x.reshape(B, H' * W' * C) concatenates H'·W'
time coordinates as if they were features. Hyperboloid.hcat, which stacks only the spatial parts
and rebuilds one time coordinate, stays on the manifold but inflates the radius.
The inflation is a dimension effect. For Gaussian-ish spatial parts \(\|v\|^2 \sim \chi^2_k\), so
which grows with the dimension \(k\). Concatenating \(N = H' \cdot W'\) blocks widens
\(k\) from \(d\) to \(N\,d\) and lifts the expected log spatial radius by \(\approx \tfrac12 \log N\)
— a radius inflation of \(\approx \sqrt{H' \cdot W'}\). LogCat
(Hyperboloid.log_radius_concat, Shi et al. 2026 Sec. 4.3) cancels it by shrinking
every block first:
then recomputing the time coordinate so the result stays on the (widened) hyperboloid.
Why this bites harder at the FC boundary than inside a conv
Inside HypConv2DHyperboloidILNN, LogCat covers a receptive field of \(N = 9\). At the
flatten, \(N\) is the entire feature map (tens to low hundreds), so a naive flatten
hands the head a point an order of magnitude farther out than its init expects. The
observed symptom, from before the MLR asinh clamp was removed, was an MLR head
whose logits all sat at that clamp at step 0 with near-zero gradients. The
symptom cleared when the flatten used LogCat.
Use hyp_flatten2d, which reshapes the grid to the per-sample point sequence and
applies LogCat for you:
import jax
import jax.numpy as jnp
from flax import nnx
from hyperbolix.manifolds import Hyperboloid
from hyperbolix.nn_layers import HypConv2DHyperboloidILNN, HypRegressionHyperboloid, hyp_flatten2d
hyperboloid, c = Hyperboloid(), 1.0
conv = HypConv2DHyperboloidILNN(
manifold_module=hyperboloid, in_channels=17, out_channels=9,
kernel_size=3, stride=2, rngs=nnx.Rngs(0),
)
head = HypRegressionHyperboloid(
manifold_module=hyperboloid, in_dim=4 * 4 * 8 + 1, out_dim=10, rngs=nnx.Rngs(1),
)
v = 0.1 * jax.random.normal(jax.random.PRNGKey(2), (8, 8, 8, 17))
x = jax.vmap(jax.vmap(jax.vmap(hyperboloid.expmap_0, in_axes=(0, None)), in_axes=(0, None)), in_axes=(0, None))(
v.at[..., 0].set(0.0), c
) # (8, 8, 8, 17) on-manifold feature map
feat = conv(x, c) # (8, 4, 4, 9) — 9 ambient = 8 spatial + time
flat = hyp_flatten2d(feat, hyperboloid, c) # (8, 129) — 4*4*8 spatial + one time
logits = head(flat, c) # (8, 10)
Width bookkeeping. hyp_flatten2d grows the ambient dimension from A per pixel
to H'·W'·(A − 1) + 1; size the head for it (in_dim = 129 above). hyp_avg_pool2d keeps
the width at A but discards the spatial layout. Both are on the
convolutional API page.
Proper Velocity: The Hyperboloid Without a Constraint¶
The Proper Velocity (PV) model (Chen et al. 2026) stores a point as the spatial part of a
hyperboloid point, in unconstrained \(\mathbb{R}^n\): any finite vector is a valid point. It has
the hyperboloid's accuracy, and no better: dist and logmap go through the exact hyperboloid
lift (see below), and its tangent-space metric shares the hyperboloid's
formulation (see below). What PV adds is convenience:
- no projection and no constraint drift after an update;
- a bounded factor \(\beta_x = 1/\sqrt{1 + c\|x\|^2} \in (0, 1]\) in place of a conformal factor that blows up at a boundary;
- plain
optaxoptimizers train PV layers without a Riemannian wrapper.
The PV distance from the origin, \(d(0, x) = \mathrm{asinh}(\sqrt{c}\,\|x\|)/\sqrt{c}\), stays finite
in float32 for \(\|x\|\) up to at least \(10^2\) (checked by test_pv_stability_at_large_norms).
import jax
import jax.numpy as jnp
from hyperbolix.manifolds import ProperVelocity
pv = ProperVelocity()
c = 1.0
# Any finite vector is a PV point; there is no boundary to project onto.
x_large = jnp.array([50.0, 0.0, 0.0])
d = pv.dist_0(x_large, c) # asinh(50) ≈ 4.61
y = pv.logmap_0(x_large, c) # finite tangent vector
x_rec = pv.expmap_0(y, c) # round-trips to x_large
Training PV layers
HypLinearPV, HypConv2DPV, and HypRegressionPV store their weights as plain nnx.Param (not ManifoldParam). Use a standard nnx.Optimizer(model, optax.adam(lr), wrt=nnx.Param) — no riemannian_adam / riemannian_sgd wrapper is required.
Where Each Model Stops¶
Pairwise operations hold to the scaled radius \(a = \sqrt{c}\,d\) below (\(c = 1\)). Past it the chart
cannot represent the point, which is not a bug. The hyperboloid has no such ceiling below
\(a \approx 45\), where computing its float32 time coordinate overflows; its float32 error grows
with the radius instead (the numbers are the median dist error over pairs at distance 1).
To choose a manifold by use case, see Choosing a manifold.
| Model | Good to \(a\) (float32 / float64) | Limit |
|---|---|---|
| Hyperboloid | float32 error \(\propto \varepsilon\sinh a\): 5e-4 at 12, 6e-2 at 16.6 / much further | the storage floor \(\varepsilon\sinh(a)/\sqrt{c}\), which reaches 1 at 16.6; the constraint can drift under Euclidean updates (The atol Convention, Known Limitations) |
| ProperVelocity | as the hyperboloid | the hyperboloid's storage floor |
| Poincaré | 12.65 / 27.73 (13.80 / 28.88 at \(c = 0.1\)) | the ball chart's ceiling; float32 is fine to \(a \approx 7\) (table) |
| κ-Stereographic, \(c > 0\) | as Poincaré | plus the Taylor band near \(c = 0\) (below) |
| Klein | 6.32 / 13.86 | half the Poincaré radius, and the floor \(\varepsilon\cosh^2(a)\) (below) |
| HalfSpace | inf/NaN past 88.7 / 709.8 |
the floor \(\approx 0.4\,(\varepsilon/2)\cosh(\sqrt{c}\,\delta)/\sqrt{c}\) (below) |
κ-Stereographic: Numerics Near Zero Curvature¶
Signed curvature adds a regime the other manifolds lack: near \(c = 0\) every closed form becomes
\(0/0\), and Stereographic switches to Taylor series. This matters when you train a signed
learnable curvature that may cross zero.
For \(c > 0\) the Poincaré sections above apply: addition, gyration, proj and the conformal
factor are the functions Poincare calls, and dist, logmap and geodesic use the
asinh form of Poincaré's default dist. dist has no counterpart of
Poincaré's saturating slot 1. geodesic(t, x, y) stores \(t \otimes ((-x) \oplus y)\), a point at
radius \(t\,d\) that float32 caps once \(\sqrt{c}\,t\,d\) passes the
chart ceiling; \(t = 1/2\) stays inside for any two points the ball
stores. See the κ-Stereographic API reference for the sign
convention and the factor-2 flat limit.
The Taylor Cutover Is dtype-Dependent¶
The curvature-generalized trig functions (\(\tan_\kappa\), \(\tan_\kappa^{-1}\)) use their exact closed forms away from zero and a truncated Taylor series (degree 5 in \(\kappa\lVert x\rVert^2\)) near zero. The cutover differs by precision:
| dtype | Taylor branch used when | Why |
|---|---|---|
| float64 | \(\lvert\kappa\rvert < 10^{-9}\) | closed forms accurate down to ~\(10^{-9}\) |
| float32 | \(\lvert\kappa\rvert < 10^{-5}\) | catastrophic cancellation in the closed-form curvature gradient below this |
In float32 the values stay accurate well below \(10^{-5}\); it is \(\partial(\cdot)/\partial c\)
computed through the closed forms that degrades. The wider float32 window gives finite,
well-behaved gradients below the cutover at the cost of a seam just above it: there the curvature
gradient has relative error of order \(\varepsilon/(\lvert\kappa\rvert\,\lVert x\rVert^2)\), with
median 1–2 % and worst case 8–30 % (up to 450 % for logmap) over 200 points with
\(\lVert x\rVert \approx 0.85\), and logmap's gradient changed sign on 1–2 of them.
The same error holds at every \(\lvert c\rvert\) above the cutover, not only next to it, and in
every Poincare operation, which has no Taylor branch: with \(u = \lvert c\rvert\,\lVert x\rVert^2\)
it is up to about \(3\varepsilon/u\), in float32 0.3 at \(u = 10^{-6}\) and 3e-3 at
\(u = 10^{-4}\) (dist, dist_0, expmap_0 and logmap_0, \(\lvert c\rvert\) from \(10^{-5}\) to 1).
It matters only while every point sits that close to the origin, for example at initialization
with a learnable curvature.
The Taylor branch is additionally gated on its convergence region \(\lvert\kappa\rvert\,\lVert x\rVert^2 < 0.01\): points at extreme chart radii (\(\lVert x\rVert \sim 1/\sqrt{\lvert\kappa\rvert}\), e.g. spherical points far from the chart origin) always keep the exact closed form, no matter how small \(\lvert\kappa\rvert\) is.
At exactly \(c = 0\) the Möbius denominator's signed factorizations have the literal curvature slope \(2s\langle x, y\rangle\) for nonzero operands, so a signed learnable curvature gets a gradient of the right sign on its flat step.
Spherical Regime (\(c < 0\)) Cautions¶
- The chart has no boundary, but it has a pole. Stereographic coordinates cover the sphere minus one point; near-antipodal pairs have chart norms \(\sim 1/\sqrt{\lvert c\rvert}\) and the metric shrinks accordingly.
antipodeitself is exact (closed form \(x/(c\lVert x\rVert^2)\)), but optimizing through near-antipodal configurations concentrates precision loss the same way the Poincaré boundary does. - Distances saturate at \(\pi R = \pi/\sqrt{\lvert c\rvert}\). Gradients of
distvanish as a pair approaches antipodal, analogous toatanhsaturation in the hyperbolic regime.
Recommendations¶
- Use
Stereographic(dtype=jnp.float64)when training a signed curvature that may cross zero, per Bachmann et al. (2020). Float32 is fine at fixed moderate curvature (\(\lvert c\rvert \gtrsim 10^{-4}\)) and moderate radii. - With
LearnableCurvature(parameterization="identity"), the default clamp \([-10, 10]\) includes 0 by design — a curvature crossing zero is a feature (the geometry interpolates hyperbolic → flat → spherical smoothly), not an error state.
Klein: Cancellation-Free Distance on a Half-Radius Chart¶
Klein stores points in the same Euclidean ball as Poincare, with the same proj, and its
two-point operations (dist, logmap, ptransp, gyro_difference) are cancellation-free. The
chart limits it: a Klein point reaches the boundary at half the Poincaré radius, and
\(g_x = 1 - c\lVert x\rVert^2\), which every formula divides by, is only as accurate as its own
rounding. Below, \(a = \sqrt{c}\,d_0\) is the scaled radius and
\(\varepsilon\) the machine epsilon (1.19e-7 in float32, 2.22e-16 in float64).
The Distance Without Cancellation¶
The textbook spellings lose digits: the hyperboloid form lifted to Klein takes acosh of a number
close to 1 for close points, and the Einstein-sum form of Zhang et al. (2026), Eq. 4, builds
\((-x)\oplus_E y\) from \(O(1)\) terms that cancel. Klein.dist follows Zhang et al. (2026), Appendix
Eqs. 12 and 14. With \(w = y - x\) (exact for close points, by Sterbenz's lemma), the Lagrange and
Einstein gamma identities give
\(N\) is a sum of two non-negative terms, and no acosh or atanh is evaluated near 1. At \(x = y\),
\(w\) is exactly zero, so dist is exactly 0 with a finite zero gradient. logmap and ptransp are
built from the same \(N\).
Measured Accuracy¶
Float32 relative error of dist (median / max over 200 pairs, dimension 8, \(c = 1\)), for \(y\) a
step of 1 % of \(x\)'s Euclidean gap to the boundary, against a high-precision evaluation at the
stored points:
| \(a\) | Klein.dist |
literal acosh |
Zhang et al. Eq. 4 (artanh) |
\(\varepsilon\cosh^2(a)\) |
|---|---|---|---|---|
| 0.5 | 3.35e-08 / 1.48e-07 | 1.18e-03 / 4.29e-03 | 5.44e-07 / 2.57e-06 | 1.5e-07 |
| 2 | 1.94e-07 / 8.29e-07 | 7.73e-02 / 1.00e+00 | 1.42e-04 / 9.33e-04 | 1.7e-06 |
| 4 | 1.19e-05 / 5.15e-05 | 1.00e+00 / 4.23e+01 | 4.33e-02 / 3.58e-01 | 8.9e-05 |
| 6 | 7.48e-04 / 3.01e-03 | 1.00e+00 / 1.04e+03 | 9.97e-01 / 9.21e+01 | 4.9e-03 |
Klein.dist's error does not depend on the separation, and it stays at or below the chart
floor \(\varepsilon\cosh^2(a)\) (last column, explained below). Klein.logmap's error is the
same size. Klein.dist forward+backward also runs in about half the
time of the Eq. 4 spelling on an A100 (1e6 pairs; at 2,048 pairs the two do not separate).
The Chart Ceiling and Floor¶
Ceiling. proj caps \(\lVert x\rVert\) at \(1/\sqrt{c} - \varepsilon^{0.75}\), the Poincaré
ball's margin. A Klein point has
\(\sqrt{c}\,\lVert x\rVert = \tanh(a)\) where a Poincaré point has \(\tanh(a/2)\), so at \(c = 1\) the
ceiling is \(a = \operatorname{atanh}(1 - \varepsilon^{0.75})\) = 6.32 in float32 and
13.86 in float64, half the Poincaré ball's 12.65 / 27.73.
poincare_to_klein, hyperboloid_to_klein and pv_to_klein do not project. Because the Klein operations floor the gap \(g_k\)
at half its value on the margin, a mapped-in point reads at its own radius up to \(a \approx 6.67\)
and as 6.67 beyond (float32, \(c = 1\); float64: 14.21). Call Klein.proj after
mapping far points in, so that the stored point is the one the operations use.
Floor. \(g_x = 1 - c\lVert x\rVert^2 = \operatorname{sech}^2(a)\). Computing it subtracts \(c\lVert x\rVert^2 \approx 1\) from 1, so \(g_x\) carries an absolute error of about \(\varepsilon\), a relative error of \(\varepsilon/g_x = \varepsilon\cosh^2(a)\). The pairwise formulas and the metric divide by \(g_x\) or its square root, so this is the chart's relative error floor. It scales as \(e^{2a}\), where the Poincaré ball's and the hyperboloid's scale as \(e^{a}\).
In practice, float32 Klein is accurate to about 1e-5 at \(a = 4\) and 1e-3 at \(a = 6\). For larger
radii use Klein(dtype=jnp.float64), or the hyperboloid.
Klein.expmap evaluates its denominator as a sum of non-negative terms, so inward steps also stay near
the chart floor. It keeps the factor \(c\) that the reference implementation's _klein_expmap omits,
so it is correct for every \(c\), not only \(c = 1\).
Half-Space: Cancellation-Free Pairwise Operations¶
HalfSpace (height \(x_n > 0\) in the last coordinate, metric \(\lVert dx\rVert^2/(c\,x_n^2)\),
origin \(o = e_n/\sqrt{c}\)) builds its two-point operations from \(w = y - x\), which is exact in
floating point for close points. The textbook distance
\(\operatorname{arcosh}\!\big(1 + \lVert y - x\rVert^2/(2x_n y_n)\big)/\sqrt{c}\) takes acosh of a
number close to 1 for close pairs and loses the separation. HalfSpace.dist evaluates the same
function as \((2/\sqrt{c})\operatorname{arsinh}(\lVert r\rVert/2)\) with
\(r = \big((y - x)/\sqrt{x_n}\big)/\sqrt{y_n}\), and logmap builds \(\theta/\sinh\theta\) from the
same \(r\). In float32 at scaled separation \(10^{-5}\), the
literal acosh has median relative error 1.00 (eager, or jitted on GPU), and HalfSpace.dist a median of at most 4.24e-8
at scaled radii from 0.5 to 12, \(c = 1\). (The reference implementation, HTorch, uses the literal
acosh, and its ptransp is not an isometry; hyperbolix implements the correct transport.)
The remaining error is a stored point's own rounding, about \(0.4\,(\varepsilon/2)\cosh(\sqrt{c}\,\delta)/\sqrt{c}\), with \(\delta\) the distance to the vertical geodesic through \(o\); along that axis it does not grow with the height.
Three cases return inf/NaN instead of a finite wrong value:
- the squared chord \(\lVert r\rVert^2\) overflows past a scaled distance \(\sqrt{c}\,d\) of 88.72 in
float32 (709.78 in float64); past it
distreturnsinf, andlogmap,ptranspandgyro_differencereturn non-finite values; logmapcan return a non-finite vector earlier, once \(x_n e^{\sqrt{c}\,d}\) passes the largest float;- an exactly vertical upward
expmapstep longer than \(\theta = \ln(1/\text{tiny})\), where tiny is the smallest normal number (87.34 in float32, 708.40 in float64), returns an infinite height with NaN horizontal coordinates. The gyro operations inherit this limit through \(\exp_o\).
The reverse-mode gradient of such a step fails earlier. For an exactly vertical upward step the
float32 jax.jacrev of expmap_0, and of gyro_difference on a vertical pair, is not finite
from \(\theta \approx 33\) on, while the value is right and float64 is finite (tried up to 80); from
\(\theta \approx 44\) forward mode fails too. A step \(10^{-6}\) off the vertical has a finite Jacobian.
The math_utils Wrappers¶
The manifolds call the transcendentals in hyperbolix.utils.math_utils, not the jnp builtins.
Each has a guard that changes neither value nor gradient inside its valid range:
coshandsinhclip their argument at \(\pm 0.99\ln(\texttt{finfo.max})\) (≈ ±87.8 in float32, ±702.7 in float64), so the value cannot overflow. Past the clip the value is constant and the gradient is exactly 0.acoshfloors its argument at \(1 + 10\,\varepsilon\), andatanhclamps it to \(\pm(1 - 10\,\varepsilon)\), where the builtin derivatives are infinite.
An argument in the clipped range means the model has already diverged. The clip only keeps that one value finite; it is not a regime to train in.
asinh and acosh: The Derivative Overflows Before the Value Does¶
JAX's derivative rules for asinh and acosh are g·rsqrt(x² + 1) and g·rsqrt(x² − 1). In
float32, x² overflows once \(|x| > 1.84\times10^{19}\), and rsqrt(inf) is 0, so the derivative
comes back exactly 0.0 while the forward value is correct and no warning is raised: at
x = 1e22, jax.grad(jnp.arcsinh) returns 0.0 instead of 1e-22. A parameter downstream of an
asinh whose argument grows exponentially stops moving.
The wrappers keep jnp.arcsinh's forward value bit for bit and spell the derivatives as
1/hypot(1, x) and 1/(√(x−1)·√(x+1)), which never form x². Every
library asinh call site — the manifolds' dist/dist_0/logmap forms, asinhc, the
proper-velocity operations, the samplers and every MLR head — goes through the wrapper. The
hyperboloid MLR reaches this regime at a large hyperplane offset (see
The MLR Score at a Large Hyperplane Offset). In your own code, use
math_utils.asinh wherever the argument can grow exponentially.
This is an upstream bug, reported as
jax-ml/jax#40634. Once JAX stops squaring the
argument, the wrappers can go and the call sites can return to jnp.arcsinh.
Lorentz Residual and Midpoint at Large Radius¶
lorentz_residual (the two-point combination behind LorentzResidual and
HypformerPositionalEncoding) and lorentz_midpoint (the weighted aggregation behind
HyperbolicFullAttention, HyperboloidGyroBatchNorm/ProperVelocityGyroBatchNorm's batch mean,
and the hyperboloid Fréchet mean) both form a raw ambient vector \(h = (h_0, h_s)\) — time
coordinate \(h_0\), spatial part \(h_s\) — and pull it back onto the sheet by dividing by
\(\sqrt{-c\,\langle h,h\rangle_L}\). The midpoint takes the absolute value
\(\sqrt{c\,\lvert\langle h,h\rangle_L\rvert}\); the residual does not, so a negative weight that
makes \(h\) spacelike gives NaN rather than a wrong point on the sheet. Computing that Minkowski
square directly,
cancels. On the sheet both terms are of size \(\lVert s\rVert^2\), the squared spatial radius of the inputs, while their difference is only \(O(1/c)\), so the float32 relative error grows as \(\varepsilon_{32}\, c\, \lVert s\rVert^2\) (\(\varepsilon_{32} \approx 1.19\times10^{-7}\)). At \(c = 0.1\) it reaches \(10^{-3}\) at a spatial radius of about 290, where the gradient's error is about three times that of the residual's output, and past \(\lVert s\rVert \sim 10^4\) the computed square flips sign.
Hyperbolix instead evaluates \(\langle h,h\rangle_L\) from exact identities, valid for any weights as long as the inputs are on the sheet. For the residual \(h = x + w\,y\) with scalar weight \(w\):
For the midpoint of \(M\) points, write \(h=\sum_m w_m x_m\), \(t_m=x_{m,0}\), \(z_m=x_{m,s}/t_m\), \(T=h_0\) and \(\bar z=h_s/T\). The normalizer is
where the equality follows from the sheet identity \(1-\lVert z_m\rVert^2=1/(c t_m^2)\). The variance is evaluated from coordinate differences, so every term is non-negative for non-negative weights. Negative weights are unsupported; an all-zero weight row returns the origin.
The residual's float32 error against float64 at \(\lVert s\rVert = 10^4\) is about \(2\times10^{-5}\) on the value and \(6\times10^{-4}\) on the gradient (\(c = 0.1\), \(x \approx y\)). The limit that remains is the difference \(x - y\) itself: when two points share a direction at \(\lVert s\rVert \gtrsim 10^4\), float32 rounding swallows the subtraction before the formula sees it, and that regime needs float64.
Busemann Coordinates at Large Radius¶
Hyperboloid.busemann(x, v, c) (the horosphere coordinate behind
HypLinearHyperboloidBusemann/HypRegressionHyperboloidBusemann and HoroPCA) is
log(√c·arg)/√c with arg = x_0 - ⟨x_s, v⟩. Along the branch aligned with the ideal direction
\(v\), x_0 and ⟨x_s,v⟩ are both \(O(\cosh a)\) and nearly equal, so the literal subtraction
cancels. With \(q=\langle x_s,v\rangle\) and \(p=x_s-qv\), the library evaluates
The direction \(v\) must be a unit vector: normalize learned directions before the call. At the
origin the spatial gradient is exactly \(-v\) in exact arithmetic. Near the origin the value is
accurate to an absolute \(\varepsilon\), not a relative one: for a point at radius 1e-8 along \(v\),
float32 returns 0 where the exact value is \(-10^{-8}\), while the gradient stays right. The batched score
nn_layers.busemann_core._busemann_score, used by the Busemann MLR/FC layers, applies the same
formula.
For a model that needs float64 only in the score, use a float64 island: cast \(\hat x\) and the class
directions to float64, run the one similarity GEMM there, compute
\(\mathrm{arg} = 1/(c(x_0+r)) + r(1-g)\), with \(r = \lVert x_s\rVert\) and
\(g = \langle \hat x_s, v\rangle\), and its log in float64, then cast back. This is a pattern, not
a library option; HyperbolicFullAttention's score_dtype (see
above) is the library's version of the same idea for the Lorentzian
score.
HoroPCA Projection at Large Radius¶
horo_projection, the ideal-point projection behind hyperbolix.decomposition.horopca, preserves
every Busemann coordinate of its input, so it has the accuracy of busemann above. HoroPCA
centres its data with Hyperboloid.gyro_difference rather than a Lorentz-boost matrix product,
which cancels for points close to a far centre: at \(a = 8\) in float32, the largest error of the
centred points is 3.7e-4 scaled nats, against 0.58 with the boost.
Gyro-Difference and GyroBatchNorm Centering at Large Radius¶
Hyperboloid.gyro_difference(x, y, c) computes \((\ominus x)\oplus y\), the operation
HyperboloidGyroBatchNorm needs to center a batch on its mean and any layer needs for the
difference of two far points. The general addition(neg(x), y) (the Lorentz boost
\(\Lambda_{\ominus x}\,y\) — see above) is
accurate almost everywhere except here: at \(y \approx x\) the boost's spatial part is a sum of three
terms, each \(O(e^{2a})\), that cancel at \(y = x\), so its absolute error is eps·cosh²(a)/√c however
close \(y\) is to \(x\).
gyro_difference instead uses the transvection carrying \(x\) back to the origin,
which the polar frame evaluates with no cancellation. What limits it at \(y \approx x\) is the
inputs: two float32 points whose directions are \(\psi\) radians apart fix the direction of their
difference only to eps32·√D/ψ relative, and a result far from the origin to one ulp of its own
radius, eps32·sinh(√c·d₀)/√c. Measured at \(a \in \{9, 12\}\), gyro_difference stays within
these floors, while the boost's error at \(a = 9\) is 0.9 to 2.6 nats whatever the true answer.
HyperboloidGyroBatchNorm and ProperVelocityGyroBatchNorm center with gyro_difference. The
boost's error there is finite and plausible, not a NaN: centering 32 points at \(a = 9\) with it
gives output errors up to 5.2 nats, against 4e-3 with gyro_difference.
Origin derivatives and the Cartesian chart¶
gyro_difference and ptransp use Cartesian formulas when either endpoint's scaled spatial
radius is at most 1e-1: the difference is the inverse Lorentz boost with reconstructed time, and
transport is the closed-form geodesic transport, with the input tangent's time coordinate taken as
\(v_0=\langle x_s,v_s\rangle/x_0\). Otherwise both use the stable geodesic frame.
logmap uses the Cartesian expression when either endpoint is exactly the origin and the polar
frame otherwise. With the frame's separation \(S=\sinh(\sqrt c\,d(x,y)/2)\), it uses the equivalent
spatial displacement \(\operatorname{asinhc}(S)((y_s-x_s)-2S^2x_s)/\sqrt{1+S^2}\) in two cases: for
close pairs (\(S\le0.5\)), and at every separation for a base point \(x\) with scaled spatial radius
\(\sqrt c\,\lVert x_s\rVert\le\) 1e-1. At coincidence the Jacobians through spatial lifts are \(+I\)
for the target and \(-I\) for the base.
The second case is there for the derivative, not for the value. The frame builds the result from two legs that each depend on \(\hat x_s\), whose derivative grows like \(1/\lVert x_s\rVert\), and only their sum is smooth in \(x_s\). The Jacobian with respect to the base point therefore lost about \(\varepsilon/(\sqrt c\,\lVert x_s\rVert)\): in float32 a relative error of 4e-4 at scaled radius 3e-4 and above 1 at 1e-7, with a correct value. With the displacement form it is within 1e-6. Just outside the neighbourhood the frame's error is up to 9e-6 and falls like \(1/\lVert x_s\rVert\).
A nonzero point closer to the origin than MIN_NORM \(=10^{-15}\) is below what the frame
resolves, because it floors the radius there. logmap still returns the right value for such a
point, but its Jacobian with respect to that point is off by order 1. The exact origin is not
affected in logmap, but it is in Hyperboloid.dist and ProperVelocity.dist: with one
endpoint at the exact origin, the distance carries a constant absolute error of MIN_NORM/2 =
5e-16 at c = 1 (a relative error of 5e-6 at radius 1e-10), and below radius 1e-15 it comes out
a factor 1.414 too large. Hyperboloid.sqdist squares that distance, so it is a factor 2 too
large there. dist_0 has no such error, so use it for a distance from the origin. In float32
the error is below rounding from radius 1e-8 up.
tangent_norm, tangent_inner and the geodesic length inside expmap have the same property
with respect to the base point: their radial/perpendicular split of the tangent vector divides by
\(\lVert x_s\rVert\). For a base point with scaled spatial radius at most 1e-1 they take the
Cartesian form of the transport to the origin,
\(v_s - \dfrac{v_0}{x_0 + 1/\sqrt c}\,x_s\) with \(v_0=\langle x_s,v_s\rangle/x_0\), whose Euclidean
products are the tangent inner products at \(x\). Before, the float32 Jacobian of expmap with
respect to such a base point was off by 7e-4 at scaled radius 1e-3 and above 1 at 1e-7 for a step
of scaled length 10, with a correct value; now it is within 1e-6. The reparameterized
wrapped_normal_hyperboloid.sample goes through this expmap at the mean. The proper-velocity
tangent_norm, tangent_inner and expmap do the same.
The polar frame never normalizes a direction that vanishes on a collinear pair (two points on one
ray, \(y = x\) included). Its angular leg is a non-negative combination of \(\hat y_s - \hat x_s\) and
\(\hat x_s + \hat y_s\), which is exactly zero on both degenerate rays, so logmap, ptransp and
gyro_difference have correct gradients there as well as correct values.
PV logmap, gyro_difference and ptransp inherit this through their exact lifts (below). The
tests check these derivatives against analytic or finite-difference references.
Two float32 derivatives with respect to c are wrong while the values are right. The second
derivative of logmap_0, expmap_0 and scalar_mul at the exact origin is NaN, on the
hyperboloid and in proper velocity (the first derivative is right). Forward-mode jax.jacfwd of
Hyperboloid.expmap is inf at c = 1e-3 for a base point at scaled radius 8 and a tangent of
scaled length 30, where jax.jacrev is right. On the Poincaré ball the float32 first derivative
with respect to c of the same three origin maps at the exact origin is NaN at c = 1e-10 (true
value 0); at c = 1e-6 and in float64 it is right.
ProperVelocity Through the Hyperboloid Lift¶
Proper-velocity coordinates are the hyperboloid's spatial part, so the lift
is exact, and a PV tangent \(v\) lifts to \((\langle x,v\rangle/X_0,\,v)\). PV dist, logmap,
expmap, gyro addition, gyro_difference and ptransp lift their inputs, run the hyperboloid
operation, and return its spatial part; no Poincaré chart conversion is involved.
tangent_inner/tangent_norm share the hyperboloid's radial_perp_decomposition. So PV has the
hyperboloid's accuracy and its remaining limits, including transport of directions below float32
angular resolution.
Measured at \(c = 0.5\): the float32 dist of a radial step of length 0.1 at \(a = 10\) has relative
error 8e-7 along a coordinate axis, about the storage floor of the input pair (1.1e-4 for one
random direction, whose floor is 1.4e-5), and the unit radial tangent's
\(\lvert\langle v,v\rangle - 1\rvert\) has median 5.2e-5 at \(a = 12\). dist(x, x) and logmap(x, x) are
exactly 0, with a finite gradient. Code built on PV dist inherits this, e.g.
utils.helpers.compute_pairwise_distances and nn_layers.poincare_batchnorm.frechet_variance
(called by ProperVelocityGyroBatchNorm).
Version Parameters¶
Several manifold operations have more than one formula for the same quantity. The version_idx
argument selects which one runs.
Poincaré Ball Distance Versions¶
from hyperbolix.manifolds import Poincare
import jax.numpy as jnp
poincare = Poincare()
x = jnp.array([0.1, 0.2])
y = jnp.array([0.3, 0.4])
c = 1.0
# Version 0: Direct Möbius distance (default)
d0 = poincare.dist(x, y, c, version_idx=poincare.VERSION_MOBIUS_DIRECT)
# Version 1: Möbius via addition
d1 = poincare.dist(x, y, c, version_idx=poincare.VERSION_MOBIUS)
# Version 2: metric-tensor form; `dist` runs version 0's body (identical values)
d2 = poincare.dist(x, y, c, version_idx=poincare.VERSION_METRIC_TENSOR)
print(f"Version 0: {d0:.6f}")
print(f"Version 1: {d1:.6f}")
print(f"Version 2: {d2:.6f}")
# 0 and 2 are identical; 1 agrees here but saturates on far pairs
| slot | dist |
dist_0 |
|---|---|---|
VERSION_MOBIUS_DIRECT (0), default |
\(\frac{2}{\sqrt c}\operatorname{arcsinh}\big(\sqrt c\lVert x-y\rVert/\sqrt{B_x B_y}\big)\), \(B_x = 1 - c\lVert x\rVert^2\) | \(\frac{2}{\sqrt c}\operatorname{atanh}(\sqrt c\lVert x\rVert)\) |
VERSION_MOBIUS (1) |
the norm of \((-x)\oplus y\); saturates on far pairs | same as slot 0 |
VERSION_METRIC_TENSOR (2) |
runs slot 0's body | the arcsinh form below |
Slot 2 Reads the Radius Through arcsinh¶
The metric-tensor distance is
\(d_0(x) = \frac{1}{\sqrt{c}}\operatorname{acosh}\big(1 + \frac{2c\lVert x\rVert^2}{1 - c\lVert x\rVert^2}\big)\),
whose whole radial signal is a small perturbation of a leading 1: through acosh's domain floor
(1 + 10·eps), every float32 radius below \(\approx 7.7\times10^{-4}/\sqrt{c}\) would read as the same
distance, \(\operatorname{acosh}(1 + 10\,\varepsilon_{32})/\sqrt{c} \approx 1.5\times10^{-3}/\sqrt{c}\), with
zero gradient. Slot 2 therefore evaluates it with the half-angle identity
\(\operatorname{acosh}(1 + 2t) = 2\operatorname{arcsinh}(\sqrt{t})\),
whose argument is linear in the radius near the origin. Its median float32 relative error against
a 60-digit reference is below 1e-7 from radius 1e-8 to \(0.9/\sqrt c\) (\(c = 1\)), and it equals
slot 0 to rounding. The pairwise dist uses the same identity with
\(t = c\lVert x - y\rVert^2/\big((1 - c\lVert x\rVert^2)(1 - c\lVert y\rVert^2)\big)\); that is
slot 0's body. dist(x, x) is exactly 0, with gradient 0.
Which Version to Use?¶
Use VERSION_MOBIUS_DIRECT (version 0), the default.
VERSION_METRIC_TENSOR(version 2): fordistit runs slot 0's body and returns the same values;dist_0's slot 2 is thearcsinhform, equal to slot 0 to rounding (see above).VERSION_MOBIUS(version 1) saturates on far pairs: in float32 it returns 12.637 for a true distance of 14.4 (two points at scaled radius 7.2 on opposite sides, \(c = 1\)), with a gradient relative error of 1.0. In float64 it saturates at \(\sqrt{c}\,d \approx 27.7\).- Far points: no version helps. From \(a = \sqrt{c}\,d \approx 10\), use float64 or the hyperboloid
(see the table); the ball cannot store a point past
\(a = 12.65\) (float32) / 27.73 (float64) at \(c = 1\) (see
The Round-Trip Ceiling). The hyperboloid has no such
ceiling up to \(a \approx 45\) at \(c = 1\), where computing the time coordinate overflows (see
Input Overflow); its float32 error grows as \(\varepsilon\sinh a\),
to a median
disterror of 5e-4 at \(a = 12\) and 6e-2 at 16.6 for pairs at distance 1. Convert withisometry_mappings.poincare_to_hyperboloid.
Hyperboloid Distance Versions¶
The Hyperboloid has its own two-way version_idx. The same two slots select an arm of both the
pairwise dist and the origin distance dist_0, which are different implementations. A
version_idx outside {0, 1} raises ValueError:
| slot | dist arm |
dist_0 arm |
floor |
|---|---|---|---|
VERSION_DEFAULT (0) |
cancellation-free hyperbolic haversine | arcsinh(√c·‖x_s‖)/√c |
none: exactly 0 at coincidence / at the origin |
VERSION_SMOOTHENED (1) |
the same, floored in quadrature | the same, with ‖x_s‖ floored in quadrature |
2·arcsinh(10·eps)/√c and arcsinh(20·eps)/√c, equal to first order: ≈2.4e-6/√c (float32), ≈4.4e-15/√c (float64) |
from hyperbolix.manifolds import Hyperboloid
import jax.numpy as jnp
hyperboloid = Hyperboloid()
x = hyperboloid.proj(jnp.array([1.0, 0.1, 0.2]), c=1.0)
y = hyperboloid.proj(jnp.array([1.0, 0.3, 0.4]), c=1.0)
c = 1.0
# VERSION_DEFAULT (0): cancellation-free hyperbolic-haversine distance — the default.
d0 = hyperboloid.dist(x, y, c, version_idx=hyperboloid.VERSION_DEFAULT)
# VERSION_SMOOTHENED (1): same evaluation, with a strictly-positive floor at coincidence.
d1 = hyperboloid.dist(x, y, c, version_idx=hyperboloid.VERSION_SMOOTHENED)
Use VERSION_DEFAULT for new code. Use VERSION_SMOOTHENED when a downstream 1/dist or
log dist would otherwise divide by zero: coincident points then return the floor above, with a
well-defined gradient.
Using Versions with JIT¶
Keep version_idx static (bake it into the function, or static_argnames): a traced index still
compiles, but every variant ends up in one lax.switch. Batch the single-point operations with
jax.vmap and compile with jax.jit; see Batching & JIT
for the patterns.
Projection¶
The library applies proj where its operations need it: Poincaré addition clamps its output,
layers such as HypLinearPoincare end in that addition, and the Riemannian optimizers move
parameters by expmap or retraction. Call proj yourself only on points you build: raw
parameters, data loaded from disk, and hand-made ambient vectors.
Checking Manifold Constraints¶
Validation Functions¶
Each manifold provides is_in_manifold for validation:
from hyperbolix.manifolds import Poincare, Hyperboloid
from hyperbolix.nn_layers import spatial_to_hyperboloid
import jax.numpy as jnp
poincare = Poincare()
hyperboloid = Hyperboloid()
# Poincaré ball: c·||x||² < 1
x = jnp.array([0.5, 0.3])
assert poincare.is_in_manifold(x, c=1.0)
# Hyperboloid: -x₀² + Σxᵢ² = -1/c (with x₀ > 0). Building the ambient point
# from its spatial part is the only way to land on the sheet exactly:
# x₀ = sqrt(||x_spatial||² + 1/c).
x_ambient = spatial_to_hyperboloid(jnp.array([0.2, 0.3, 0.1]), 1.0, 1.0) # (dim+1,)
assert hyperboloid.is_in_manifold(x_ambient, c=1.0)
The atol Convention¶
is_in_manifold and is_in_tangent_space take atol: float | None = None. Left as None, every
manifold resolves it through
hyperbolix.manifolds._base.default_atol(dtype) = sqrt(finfo(dtype).eps) — 3.45e-4 in float32,
1.49e-8 in float64. An explicit value is used as given: it is never floored, clamped or ignored.
Ball membership tests the dimensionless residual c||x||² - 1, so one tolerance means the same
thing at every curvature. On the hyperboloid the check compares the stored x₀ with
√(1/c + ‖x_s‖²) and uses the tolerance as both rtol and atol, so on-sheet points pass the
default at any radius (see above).
Batch Validation¶
import jax
import jax.numpy as jnp
from hyperbolix.manifolds import Poincare
poincare = Poincare()
def validate_batch(x_batch, c=1.0, atol=1e-5):
"""Check if all points in batch satisfy manifold constraint."""
valid = jax.vmap(lambda x: poincare.is_in_manifold(x, c, atol))(x_batch)
num_valid = jnp.sum(valid)
total = len(x_batch)
if num_valid < total:
print(f"WARNING: {total - num_valid}/{total} points off manifold")
violations = jnp.where(~valid)[0]
print(f"Violating indices: {violations[:10]}") # Show first 10
return jnp.all(valid)
Best Practices Summary¶
Numerical Stability Checklist
- Choose the dtype by the scaled radius \(a = \sqrt{c}\,d\). On the Poincaré ball, float32 is fine to \(a \approx 10\); use float64 for critical operations from there, and float64 is required past the float32 chart ceiling (\(a \approx 12.65\) at \(c = 1\); see the table).
- For large radii, use the hyperboloid or
ProperVelocity. The hyperboloid's float32 error grows as \(\varepsilon\sinh a\) (5e-4 at \(a = 12\), 6e-2 at 16.6; see Known Limitations);ProperVelocitylives in unconstrained \(\mathbb{R}^n\) and trains with plainoptax.adam. - Use
VERSION_MOBIUS_DIRECTfor Poincaré distance (the default).VERSION_MOBIUSsaturates on far pairs, past \(\sqrt{c}\,d \approx 12.6\) in float32 and 27.7 in float64 at \(c = 1\). - Learn curvature with
LearnableCurvature(default log parameterization, bounds \([\texttt{init\_c}/10,\ \texttt{init\_c}\cdot10]\); its clamp passes only gradients that point back inside). Don'tjnp.clipthe curvature yourself: past the bound the gradient ofjnp.clipis 0, and the curvature stops moving. - Keep the default initializations. A too-small init freezes a deep stack without any NaN; see Initialization Scales.
- A NaN loss is the intended signal of divergence. Don't clip or clamp points, inputs or curvature to keep values finite; find where the value diverged instead (below).
Debugging Numerical Issues¶
-
Check for NaN/Inf:
-
Verify manifold constraints with
validate_batchfrom above: -
Check the scaled radius of your points against the model's limit (the Quick Check computes it for the Poincaré ball).
-
Switch to float64: enable
jax.config.update("jax_enable_x64", True)and build the manifold withdtype=jnp.float64— casting only the data does nothing, since a float32 manifold casts its inputs back. Switching the Poincaréversion_idxdoes not help (see Which Version to Use?):
See Also¶
- Batching & JIT: Performance optimization patterns
- Manifolds API: Manifold function reference
- Training Workflows: End-to-end training examples