Embedding¶
LorentzEmbedding is a token embedding table whose rows are points on the hyperboloid
(the LorentzEmbeddings table of HELM, He et al. 2025). Two parameterizations are
available: a ManifoldParam table of ambient points trained with
riemannian_adam, or a Euclidean table of spatial coordinates whose
time coordinate is rebuilt in the forward pass and trained with any Euclidean optimizer.
lorentz_embedding_init builds the manifold-parameterized table.
hyperbolix.nn_layers.LorentzEmbedding ¶
LorentzEmbedding(
num_embeddings: int,
features: int,
*,
rngs: Rngs,
c: float = 1.0,
parameterization: Literal[
"manifold", "spatial"
] = "manifold",
init_radius: float = 1.0,
param_dtype: DTypeLike = jnp.float32,
)
Bases: Module
Token embedding on the hyperboloid (HELM's LorentzEmbeddings without positional encoding).
Looks up rows of a (V, A) table of hyperboloid points at curvature c and, when
c_out is given, maps them onto the c_out sheet by scaling the whole ambient vector by
sqrt(c/c_out) (HELM: emb * sqrt(k_out/k_in)). That map keeps the scaled radius
sqrt(c)·d of every row.
Two parameterizations, matching HELM's two configurations:
"manifold"(HELMproject_emb=0, the 120M model): the table is a :class:~hyperbolix.optim.ManifoldParamof shape(V, A)onHyperboloidatc, initialized by :func:lorentz_embedding_init(isotropic directions at geodesic radiusinit_radius, the clean form of HELM'srandom_normalinit). Train it with :func:~hyperbolix.optim.riemannian_adam, whichnnx.Optimizer(model, riemannian_adam(lr), wrt=nnx.Param)applies to this leaf while every othernnx.Paramtakes a plain Adam step. HELM uses the model learning rate and weight decay 0 for the table (and geoopt'sstabilize=1, whichriemannian_adamalready does at every step; see its docstring), and AdamW for the Euclidean parameters."spatial"(HELMproject_emb=1, the 1B model): a Euclideannnx.Paramof shape(V, A - 1)withN(0, 1)entries (torch.nn.Embedding's init) holding the spatial coordinates atc; the forward pass rebuilds the time slot with :func:~hyperbolix.nn_layers.hyperboloid_core.spatial_to_hyperboloid. Train it with a Euclidean optimizer. Its rows start at spatial norm≈ sqrt(A - 1), i.e. geodesic radius≈ asinh(sqrt(c·(A - 1)))/sqrt(c)(4.0 atA = 768,c = 1). HELM's projection rebuilds time atk_inand applies noc_outmap; herec_outacts as in the manifold path.
The table's curvature c is fixed at construction: a ManifoldParam lives on one sheet,
and the optimizer reads its curvature from the parameter's metadata.
Args:
num_embeddings: Vocabulary size V.
features: Ambient dimension A of the output points (spatial dim + 1, >= 2).
rngs: NNX RNG streams; the init draws from rngs.params().
c: Curvature the table is stored on (positive, default 1.0).
parameterization: "manifold" (default) or "spatial", see above.
init_radius: Geodesic radius of every row at init for "manifold" (default 1.0, HELM's
scale); ignored for "spatial".
param_dtype: Storage dtype of the table (default jnp.float32, pinned so it does not
become float64 under jax_enable_x64). The output has this dtype.
Attributes:
embedding: The table, a ManifoldParam (V, A) or an nnx.Param (V, A - 1).
manifold: The Hyperboloid instance the "manifold" table is tagged with.
Source code in hyperbolix/nn_layers/hyperboloid_embedding.py
__call__ ¶
__call__(
ids: Int[Array, ...],
c_out: float | Float[Array, ""] | None = None,
) -> Float[Array, "... A"]
Look up token ids.
Args:
ids: Integer token ids of any shape (...), with V = num_embeddings. Ids
>= V or < -V give NaN rows (jnp.take's fill mode) rather than a
silently clamped valid row. Ids from -V to -1 count from the end, like
Python indices, and return valid rows: a -1 padding id gets the last token's
row without an error.
c_out: Curvature of the output sheet; None (default) keeps the table's c. May be
a traced value (e.g. from LearnableCurvature).
Returns:
Points of shape (..., A) on the hyperboloid of curvature c_out (or c).
Source code in hyperbolix/nn_layers/hyperboloid_embedding.py
hyperbolix.nn_layers.lorentz_embedding_init ¶
lorentz_embedding_init(
key: Array,
num_embeddings: int,
features: int,
c: float,
*,
radius: float = 1.0,
dtype: DTypeLike = jnp.float32,
) -> Float[Array, "V A"]
Hyperboloid table with isotropic directions at a fixed geodesic radius.
Each row is Hyperboloid.expmap_0((0, radius·u)) at curvature c, with u drawn
uniformly from the spatial unit sphere (a normalized standard-normal draw over the S = A - 1
spatial coordinates). Every row therefore sits at geodesic distance radius from the origin,
i.e. scaled radius sqrt(c)·radius.
This is the clean form of HELM's init. HELM (Lorentz.random_normal → geoopt expmap0 +
project) normalizes a standard-normal draw over all A coordinates, time included, and
passes that non-tangent vector to expmap0, whose Minkowski norm n = sqrt(‖u_s‖² - u_0²)
it clamps at 1e-8 before the projection rebuilds the time slot. With u_0² ~ 1/A the
resulting radius is 1 - O(u_0²) at every k (measured deficit 1 - d between
u_0²/2 and u_0²; all rows within 0.012 of 1 at A = 768, down to 0.31 at A = 8,
where 3 % of rows hit the clamp; logs/2026-09-30_helm-embedding/probe_reference_radius.out).
The intended radius is 1 in geodesic distance, independent of the curvature, which is the
default here.
Args:
key: PRNG key for the direction draw.
num_embeddings: Vocabulary size V.
features: Ambient dimension A (>= 2).
c: Curvature the table is stored on (positive).
radius: Geodesic distance of every row from the origin (default 1.0, HELM's scale).
dtype: Storage dtype of the table.
Returns:
Table of shape (V, A) on the hyperboloid of curvature c.
Source code in hyperbolix/nn_layers/hyperboloid_embedding.py
Example¶
import jax.numpy as jnp
import optax
from flax import nnx
from hyperbolix.nn_layers import LorentzEmbedding
from hyperbolix.optim import riemannian_adam
emb = LorentzEmbedding(num_embeddings=100, features=9, rngs=nnx.Rngs(0), c=1.0)
ids = jnp.array([[3, 7, 7], [1, 0, 42]])
points = emb(ids) # (2, 3, 9) on the hyperboloid at c = 1
points_c2 = emb(ids, c_out=2.0) # the same rows mapped onto the c = 2 sheet
# The manifold table takes a Riemannian step; other nnx.Params take a plain Adam step.
optimizer = nnx.Optimizer(emb, riemannian_adam(1e-3), wrt=nnx.Param)