Type at least 2 letters. Enter opens the first result, Esc closes.

    ML for Science · Lesson 15 · Stochastic and generative models

    Flow matching on the sphere (Riemannian flow matching)

    Advanced 80 min 4 interactive figures 3 videos Colab notebook

    Before this lesson: flow matching in Rd\R^d: the marginalization trick, the CFM loss and Euler sampling (lesson 14); coordinates on the sphere (lesson 09); matrix manifolds, tangent spaces and projections (linear algebra 19). Time: about 80 minutes, plus the notebook.


    Why this matters #

    • Events on the Earth. Earthquake epicentres, volcanic eruptions, floods and wildfires are points on the sphere. Chen and Lipman (ICLR 2024) test Riemannian flow matching on exactly these four Earth-science catalogues (compiled by Mathieu and Nickel, 2020). A density model on the sphere gives hazard maps and synthetic catalogues.
    • Other curved spaces. Wind directions live on a circle, protein backbone angles on a torus, and the orientation of a rigid body (or of a fault plane with its slip direction) in the rotation group SO(3)SO(3). The same tools (exp, log, tangent projections, retractions) build flow models on all of them.
    • No new training cost. On the sphere, geodesics have a closed form, so training stays simulation-free, as in lesson 14. In flat space the method is exactly the CFM of lesson 14.

    Do not confuse this lesson with lesson 09. Lesson 09 is about fields on the sphere (a value at every point, such as temperature). This lesson is about probability distributions of points on the sphere (where events happen).

    1. Predict first #

    2. The picture #

    The simplest case: the circle #

    A point on the circle is an angle φ\varphi, and a velocity is one number. The shortest way from φ0\varphi_0 to φ1\varphi_1 is the shorter arc, of length ∣wrap⁡(φ1−φ0)∣\lvert\operatorname{wrap}(\varphi_1 - \varphi_0)\rvert, where wrap⁡\operatorname{wrap} maps an angle to (−π,π](-\pi, \pi]. Moving with velocity vv for unit time gives φ+v\varphi + v, and the velocity that reaches φ1\varphi_1 in unit time is wrap⁡(φ1−φ0)\operatorname{wrap}(\varphi_1 - \varphi_0). So flow matching on the circle is the flow matching of lesson 14 with one change: the difference x1−x0x_1 - x_0 becomes the wrapped difference.

    At one point, φ1=φ0+π\varphi_1 = \varphi_0 + \pi, the two arcs are equally short and the wrapped difference jumps from −π-\pi to +π+\pi. That point is the trouble spot on every sphere.

    The sphere: tangent planes and great circles #

    A point is a unit vector x∈R3x \in \R^3. The velocities of curves through xx form the tangent plane: the vectors orthogonal to xx. A straight line x+svx + sv leaves the sphere at once. The "straight lines" of the sphere are the great circles: walk a distance ss from xx in the unit tangent direction w^\hat w and you arrive at cos⁡(s) x+sin⁡(s) w^\cos(s)\,x + \sin(s)\,\hat w.

    Interactive figure: loading…

    Move the direction and the length of vv; then press "|v| > π". The point xx (dark), its tangent plane (blue patch) and a tangent vector vv (orange, drawn at half length). exp⁡x(v)\exp_x(v) walks the distance ∣v∣\lvert v\rvert along the great circle (yellow) in the direction of vv. Violet: log⁡x(exp⁡xv)\log_x(\exp_x v), the way back. For ∣v∣<π\lvert v\rvert < \pi it equals vv (the arrows overlap). For ∣v∣>π\lvert v\rvert > \pi the walk passes the opposite point, and the log gives the shorter way round: a different vector. Drag the sphere to turn it.

    The recipe #

    Riemannian flow matching is lesson 14 with three replacements:

    Lesson 14 (Rd\R^d) On the sphere
    xt=(1−t) x0+t x1x_t = (1 - t)\,x_0 + t\,x_1 xt=exp⁡x0(tlog⁡x0x1)x_t = \exp_{x_0}(t\log_{x_0}x_1), the great-circle path
    target velocity x1−x0x_1 - x_0 log⁡xt(x1)/(1−t)\log_{x_t}(x_1)/(1 - t), tangent at xtx_t
    Euler step x+hvx + hv exp⁡x(hv)\exp_x(hv), or (x+hv)/∥x+hv∥(x + hv)/\lVert x + hv\rVert

    and one constraint: the network's velocity must be tangent, so we project its output onto the tangent plane.

    Quick check

    A network outputs w=(0.3,0,0.4)w = (0.3, 0, 0.4) at the north pole x=(0,0,1)x = (0, 0, 1). Which tangent velocity does the model use after the projection Px=I−xx⊤P_x = I - xx^\top?

    3. Definitions and main results #

    The sphere as a Riemannian manifold #

    • S2={x∈R3:∥x∥=1}S^2 = \{x \in \R^3 : \lVert x\rVert = 1\}. In this track (lesson 09), a point with colatitude θ∈[0,π]\theta \in [0, \pi] and longitude ϕ\phi is x=(sin⁡θcos⁡ϕ, sin⁡θsin⁡ϕ, cos⁡θ)x = (\sin\theta\cos\phi,\ \sin\theta\sin\phi,\ \cos\theta). With latitude 90°−θ90° - \theta, this is the usual map coordinate.
    • Tangent plane. For a curve c(t)c(t) on S2S^2, differentiate c⋅c=1c \cdot c = 1: 2 c⋅c˙=02\,c \cdot \dot c = 0. So TxS2={v∈R3:x⋅v=0}T_xS^2 = \{v \in \R^3 : x \cdot v = 0\}, and the orthogonal projection onto it is Px=I−xx⊤P_x = I - xx^\top, that is, Pxw=w−(x⋅w) xP_xw = w - (x \cdot w)\,x.
    • Metric. The inner product of two tangent vectors is their dot product in R3\R^3 (the sphere is a Riemannian submanifold of R3\R^3). The length of a curve is ∫∥c˙∥ dt\int\lVert\dot c\rVert\,dt. The geodesic distance is the length of the shortest curve: d(x,y)=arccos⁡(x⋅y)d(x, y) = \arccos(x \cdot y), the angle between xx and yy (times the radius, 6371 km, on the Earth).
    • Geodesics are curves with no tangential acceleration. For a unit xx and a tangent v≠0v \ne 0, c(t)=cos⁡(t∥v∥) x+sin⁡(t∥v∥) v/∥v∥c(t) = \cos(t\lVert v\rVert)\,x + \sin(t\lVert v\rVert)\,v/\lVert v\rVert has c¨=−∥v∥2c\ddot c = -\lVert v\rVert^2 c. This is normal to the sphere, so Pc c¨=0P_c\,\ddot c = 0: great circles at constant speed are the geodesics.
    • The exponential map exp⁡x(v)\exp_x(v) is the point reached at time 1 by the geodesic that starts at xx with velocity vv. The logarithm map log⁡x(y)\log_x(y) is its inverse: the shortest tangent vector vv with exp⁡x(v)=y\exp_x(v) = y. In Rd\R^d: exp⁡x(v)=x+v\exp_x(v) = x + v and log⁡x(y)=y−x\log_x(y) = y - x.

    Theorem 1 (exponential and logarithm maps of the sphere) #

    For x∈S2x \in S^2 and v∈TxS2v \in T_xS^2,

    exp⁡x(v)=cos⁡(∥v∥) x+sin⁡(∥v∥) v∥v∥(exp⁡x(0)=x).\exp_x(v) = \cos(\lVert v\rVert)\,x + \sin(\lVert v\rVert)\,\frac{v}{\lVert v\rVert} \qquad (\exp_x(0) = x).

    For y∈S2y \in S^2 with y≠−xy \ne -x, let ω=arccos⁡(x⋅y)∈[0,π)\omega = \arccos(x \cdot y) \in [0, \pi) and u=Pxy=y−(x⋅y) xu = P_xy = y - (x \cdot y)\,x. Then

    log⁡x(y)=ω u∥u∥(log⁡x(x)=0),∥u∥=sin⁡ω,\log_x(y) = \omega\,\frac{u}{\lVert u\rVert} \quad (\log_x(x) = 0), \qquad \lVert u\rVert = \sin\omega,

    and exp⁡x(log⁡xy)=y\exp_x(\log_xy) = y, ∥log⁡xy∥=d(x,y)\lVert\log_xy\rVert = d(x, y), and log⁡x(exp⁡xv)=v\log_x(\exp_xv) = v whenever ∥v∥<π\lVert v\rVert < \pi.

    1. exp⁡x(v)\exp_x(v) is c(1)c(1) for the great circle above, with c(0)=xc(0) = x and c˙(0)=v\dot c(0) = v. It has norm 1 because x⊥vx \perp v: cos⁡2+sin⁡2=1\cos^2 + \sin^2 = 1.
    2. Let y=exp⁡x(v)y = \exp_x(v) with 0<∥v∥<π0 < \lVert v\rVert < \pi. Take the dot product with xx: x⋅y=cos⁡∥v∥x \cdot y = \cos\lVert v\rVert. Since arccos⁡\arccos is one-to-one on [0,π][0, \pi], ∥v∥=arccos⁡(x⋅y)=ω\lVert v\rVert = \arccos(x \cdot y) = \omega.
    3. The tangent part of yy is u=y−(x⋅y)x=sin⁡∥v∥  v/∥v∥u = y - (x \cdot y)x = \sin\lVert v\rVert\;v/\lVert v\rVert. Since sin⁡∥v∥>0\sin\lVert v\rVert > 0, the unit vectors agree: u/∥u∥=v/∥v∥u/\lVert u\rVert = v/\lVert v\rVert. With step 2, v=ω u/∥u∥v = \omega\,u/\lVert u\rVert, which is the formula for log⁡x(y)\log_x(y), and ∥u∥=sin⁡ω\lVert u\rVert = \sin\omega.
    4. Conversely, exp⁡x(ω u/∥u∥)=cos⁡ω x+sin⁡ω u/sin⁡ω=(x⋅y) x+u=y\exp_x(\omega\,u/\lVert u\rVert) = \cos\omega\,x + \sin\omega\,u/\sin\omega = (x \cdot y)\,x + u = y. ■\blacksquare

    Where log fails. At y=−xy = -x, u=0u = 0, and every tangent direction gives a geodesic of length π\pi to −x-x: log⁡x\log_x is not defined there. The set of points where the shortest geodesic from xx stops being unique is the cut locus of xx; on the sphere it is the single point −x-x. Near it, the direction of log⁡x(y)\log_x(y) jumps when yy moves a tiny distance.

    Interactive figure: loading…

    Turn β, then make δ small. xx is the north pole. Two points yy and y′y' sit at distance δ\delta from the south pole, on opposite sides. They are only 2δ2\delta apart, but their log vectors at xx (arrows, half length) point in opposite directions, each of length π−δ\pi - \delta: the shortest great circle goes down one side of the sphere or the other. As β turns, log⁡x(y)\log_x(y) turns all the way round. A training target built from log⁡\log is discontinuous at the antipode.

    Geodesic interpolation. Put u/∥u∥=(x1−cos⁡ω x0)/sin⁡ωu/\lVert u\rVert = (x_1 - \cos\omega\,x_0)/\sin\omega into exp⁡x0(tlog⁡x0x1)\exp_{x_0}(t\log_{x_0}x_1) and use sin⁡(ω−tω)=sin⁡ωcos⁡tω−cos⁡ωsin⁡tω\sin(\omega - t\omega) = \sin\omega\cos t\omega - \cos\omega\sin t\omega:

    xt=exp⁡x0 ⁣(tlog⁡x0x1)=sin⁡((1−t)ω) x0+sin⁡(tω) x1sin⁡ω,ω=d(x0,x1).x_t = \exp_{x_0}\!\big(t\log_{x_0}x_1\big) = \frac{\sin\big((1 - t)\omega\big)\,x_0 + \sin(t\omega)\,x_1}{\sin\omega}, \qquad \omega = d(x_0, x_1).

    This is slerp (spherical linear interpolation). It runs along the great circle at the constant speed ω\omega, from x0x_0 at t=0t = 0 to x1x_1 at t=1t = 1.

    Theorem 2 (the velocity of the geodesic path) #

    For x1≠−x0x_1 \ne -x_0 and 0≤t<10 \le t < 1:

    x˙t=log⁡xt(x1)1−t,∥x˙t∥=ω=d(x0,x1).\dot x_t = \frac{\log_{x_t}(x_1)}{1 - t}, \qquad \lVert\dot x_t\rVert = \omega = d(x_0, x_1).

    Why it is true, in words. At time tt the point still has the distance (1−t)ω(1 - t)\omega to go along the same great circle, and it must cover it in the time 1−t1 - t. The log map points along the circle and has the length of the remaining distance; dividing by the remaining time gives the speed ω\omega.

    Proof of Theorem 2
    1. xt=exp⁡x0(tv)x_t = \exp_{x_0}(tv) with v=log⁡x0(x1)v = \log_{x_0}(x_1) is a geodesic with constant speed ∥v∥=ω\lVert v\rVert = \omega.
    2. The rest of the path, s↦xt+s(1−t)s \mapsto x_{t + s(1 - t)} for s∈[0,1]s \in [0, 1], is again a geodesic. It starts at xtx_t, ends at x1x_1, and its length is (1−t)ω<π(1 - t)\omega < \pi.
    3. By Theorem 1, the only tangent vector at xtx_t of norm below π\pi whose geodesic reaches x1x_1 at time 1 is log⁡xt(x1)\log_{x_t}(x_1). So the initial velocity of the curve in step 2 is log⁡xt(x1)\log_{x_t}(x_1).
    4. By the chain rule, that initial velocity is (1−t) x˙t(1 - t)\,\dot x_t. Hence x˙t=log⁡xt(x1)/(1−t)\dot x_t = \log_{x_t}(x_1)/(1 - t). ■\blacksquare

    This is the conditional velocity ut(x∣x1)=log⁡x(x1)/(1−t)u_t(x \mid x_1) = \log_x(x_1)/(1 - t) of Chen and Lipman. In Rd\R^d, log⁡xt(x1)/(1−t)=(x1−xt)/(1−t)=x1−x0\log_{x_t}(x_1)/(1 - t) = (x_1 - x_t)/(1 - t) = x_1 - x_0: the CFM target of lesson 14. The geodesic path is the sphere's version of a straight path: one exponential-map step from x0x_0 with the velocity log⁡x0(x1)\log_{x_0}(x_1) lands exactly on x1x_1.

    Interactive figure: loading…

    Move tt, then try the presets. Blue: the geodesic path from x0x_0 to x1x_1. Yellow: xtx_t; orange: its velocity log⁡xt(x1)/(1−t)\log_{x_t}(x_1)/(1 - t) at half length. The speed readout is the same for every tt: it is the distance ω\omega. Magenta dashed: the straight chord (1−t)x0+tx1(1 - t)x_0 + tx_1 of flow matching in R3\R^3. It runs inside the ball: from the North Pole to the equator, its midpoint is 1866 km below the surface.

    Quick check

    x0x_0 is the North Pole and x1=(1,0,0)x_1 = (1, 0, 0) is on the equator. What is the velocity of the geodesic path at t=0t = 0?

    Theorem 3 (Riemannian conditional flow matching) #

    Setup. A source pp on S2S^2 (for example uniform), data qq on S2S^2, a coupling of the two (independent by default), t∼U[0,1]t \sim U[0, 1], the path xt=exp⁡x0(tlog⁡x0x1)x_t = \exp_{x_0}(t\log_{x_0}x_1), and the conditional target ut(xt∣x1)=log⁡xt(x1)/(1−t)u_t(x_t \mid x_1) = \log_{x_t}(x_1)/(1 - t). Let ptp_t be the law of xtx_t, and ut(x)=E[ut(xt∣x1)∣xt=x]u_t(x) = \mathbb E[u_t(x_t \mid x_1) \mid x_t = x] the marginal velocity. Norms of tangent vectors are Euclidean norms in R3\R^3.

    • Marginalization trick on manifolds (Flow Matching Guide, Theorem 10, under its regularity assumptions): utu_t generates ptp_t, so the ODE x˙=ut(x)\dot x = u_t(x) started from pp ends at qq. The proof is Theorem 1 of lesson 14 with test functions on the sphere.
    • RCFM (Flow Matching Guide, Theorem 11): for a velocity network vθv_\theta, let LRFM(θ)=Et, x∼pt∥vθ(x,t)−ut(x)∥2L_{\mathrm{RFM}}(\theta) = \mathbb E_{t,\,x \sim p_t}\lVert v_\theta(x, t) - u_t(x)\rVert^2 and LRCFM(θ)=Et, x0, x1∥vθ(xt,t)−ut(xt∣x1)∥2L_{\mathrm{RCFM}}(\theta) = \mathbb E_{t,\,x_0,\,x_1}\lVert v_\theta(x_t, t) - u_t(x_t \mid x_1)\rVert^2. Then LRCFM(θ)=LRFM(θ)+constL_{\mathrm{RCFM}}(\theta) = L_{\mathrm{RFM}}(\theta) + \text{const}: the same gradients and the same minimizer utu_t.

    Proof of the RCFM statement. It is the proof of Theorem 2 in lesson 14, word for word:

    1. Expand ∥vθ−ut(xt∣x1)∥2=∥vθ∥2−2⟨vθ,ut(xt∣x1)⟩+∥ut(xt∣x1)∥2\lVert v_\theta - u_t(x_t \mid x_1)\rVert^2 = \lVert v_\theta\rVert^2 - 2\langle v_\theta, u_t(x_t \mid x_1)\rangle + \lVert u_t(x_t \mid x_1)\rVert^2.
    2. E∥vθ(xt,t)∥2\mathbb E\lVert v_\theta(x_t, t)\rVert^2 is the same in both losses, because xt∼ptx_t \sim p_t.
    3. Cross term, by the tower property: E⟨vθ(xt,t),ut(xt∣x1)⟩=E⟨vθ(xt,t),ut(xt)⟩\mathbb E\langle v_\theta(x_t, t), u_t(x_t \mid x_1)\rangle = \mathbb E\langle v_\theta(x_t, t), u_t(x_t)\rangle. All vectors here lie in the same tangent plane TxtS2T_{x_t}S^2, where the inner product is the one of R3\R^3.
    4. The remaining terms do not contain θ\theta. ■\blacksquare

    Nothing in the proof uses flatness. Two practical conditions:

    1. The model must be tangent: vθ(x,t)=Pxnet⁡θ(x,t)v_\theta(x, t) = P_x\operatorname{net}_\theta(x, t) for any network net⁡θ\operatorname{net}_\theta with output in R3\R^3. Otherwise the learned ODE pushes samples off the sphere.
    2. Avoid antipodal pairs: x1=−x0x_1 = -x_0 has probability zero for continuous pp and qq, but near-antipodal pairs give unstable targets; skip or clamp them.

    Simulation-free. On the sphere, xtx_t and its target have closed forms, so a training step needs no ODE solve: sample (x0,x1,t)(x_0, x_1, t), compute xtx_t and the target, regress. On a general manifold (a triangle mesh, say) geodesics have no closed form. Chen and Lipman then build a distance from the eigenfunctions of the Laplace–Beltrami operator (on the sphere these would be the spherical harmonics of lesson 09), and they must simulate the conditional path during training.

    Chen and Lipman's general formula (premetrics)

    Chen and Lipman define the conditional flow through a premetric d(x,x1)d(x, x_1) (zero only at x=x1x = x_1) and a decreasing scheduler κ(t)\kappa(t) with κ(0)=1\kappa(0) = 1, κ(1)=0\kappa(1) = 0. The flow is built so that d(xt,x1)=κ(t) d(x0,x1)d(x_t, x_1) = \kappa(t)\,d(x_0, x_1), and its velocity is

    ut(x∣x1)=dlog⁡κ(t)dt  d(x,x1) ∇d(x,x1)∥∇d(x,x1)∥2.u_t(x \mid x_1) = \frac{d\log\kappa(t)}{dt}\; d(x, x_1)\, \frac{\nabla d(x, x_1)}{\lVert\nabla d(x, x_1)\rVert^2}.

    With κ(t)=1−t\kappa(t) = 1 - t and the geodesic distance, ∇xd(x,x1)=−log⁡x(x1)/d(x,x1)\nabla_xd(x, x_1) = -\log_x(x_1)/d(x, x_1) has norm 1, and the formula gives −11−t d⋅(−log⁡x(x1)/d)=log⁡x(x1)/(1−t)\frac{-1}{1 - t}\,d \cdot \big(-\log_x(x_1)/d\big) = \log_x(x_1)/(1 - t): Theorem 2 again. With a spectral distance on a mesh the same formula still works, but xtx_t has no closed form.

    Sampling on the sphere #

    The exact flow of a tangent field stays on the sphere: ddt∥x∥2=2 x⋅v=0\frac{d}{dt}\lVert x\rVert^2 = 2\,x \cdot v = 0. Numerical steps of size hh behave differently:

    • Plain Euler x+hvx + hv: since x⋅v=0x \cdot v = 0, ∥x+hv∥2=1+h2∥v∥2>1\lVert x + hv\rVert^2 = 1 + h^2\lVert v\rVert^2 > 1. Every step moves outward, and the error adds up.
    • Exponential-map Euler xk+1=exp⁡xk(h vθ(xk,tk))x_{k+1} = \exp_{x_k}\big(h\,v_\theta(x_k, t_k)\big): a step along the great circle.
    • Projected Euler xk+1=(xk+hvk)/∥xk+hvk∥x_{k+1} = (x_k + hv_k)/\lVert x_k + hv_k\rVert: a retraction, that is, a cheap map from the tangent plane back to the manifold that matches the exponential map at least to first order. This one is even better. It moves by the angle arctan⁡(h∥vk∥)\arctan(h\lVert v_k\rVert) instead of h∥vk∥h\lVert v_k\rVert, and since arctan⁡s=s−s3/3+…\arctan s = s - s^3/3 + \dots, it matches the exponential map to second order.

    Both keep the samples on the sphere up to round-off, and both are first-order methods like Euler. The RiemannianODESolver of Meta's flow_matching library projects the velocity onto the tangent plane and the point back onto the sphere after each Euler, midpoint or RK4 step.

    Interactive figure: loading…

    Move the number of steps NN and change the target. All three samplers follow the conditional velocity log⁡x(x1)/(1−t)\log_x(x_1)/(1 - t) from the North Pole to x1x_1. Magenta: plain Euler, whose points float above the surface (norm 1.86 after one step to the equator). Orange: projected Euler; it stays on the sphere but lags behind, and the lag shrinks quickly with NN. Blue: exponential-map Euler, exact for every NN, because each step covers the fraction h/(1−tk)h/(1 - t_k) of the remaining distance along the same great circle.

    Quick check

    One plain Euler step of size h=1h = 1 from the North Pole with the velocity (π/2,0,0)(\pi/2, 0, 0). What is the norm of the new point?

    Answers to section 1. (1) At the origin: x1/2=(x0+x1)/2=0x_{1/2} = (x_0 + x_1)/2 = 0. In general, ∥(1−t)x0+tx1∥2=1−2t(1−t)(1−x0⋅x1)<1\lVert(1 - t)x_0 + tx_1\rVert^2 = 1 - 2t(1 - t)(1 - x_0 \cdot x_1) < 1 for 0<t<10 < t < 1 and x1≠x0x_1 \ne x_0. So the paths run through the interior of the ball, the network must learn a 3D flow for 2D data, and nothing forces the final samples onto the sphere. (2) Outside: ∥x+hv∥2=1+h2∥v∥2\lVert x + hv\rVert^2 = 1 + h^2\lVert v\rVert^2.

    4. Worked example #

    (a) From the North Pole to the equator. x0=(0,0,1)x_0 = (0, 0, 1) and x1=(1,0,0)x_1 = (1, 0, 0) (latitude 0, longitude 0).

    1. Distance: ω=arccos⁡(x0⋅x1)=arccos⁡0=π/2≈1.5708\omega = \arccos(x_0 \cdot x_1) = \arccos 0 = \pi/2 \approx 1.5708. On the Earth that is 6371×π/2≈10 0086371 \times \pi/2 \approx 10\,008 km.
    2. Tangent part: u=x1−(x0⋅x1) x0=(1,0,0)u = x_1 - (x_0 \cdot x_1)\,x_0 = (1, 0, 0), with ∥u∥=1=sin⁡ω\lVert u\rVert = 1 = \sin\omega. So log⁡x0(x1)=π2(1,0,0)\log_{x_0}(x_1) = \tfrac{\pi}{2}(1, 0, 0).
    3. Path: xt=cos⁡(tπ/2) x0+sin⁡(tπ/2) (1,0,0)=(sin⁡(tπ/2), 0, cos⁡(tπ/2))x_t = \cos(t\pi/2)\,x_0 + \sin(t\pi/2)\,(1, 0, 0) = (\sin(t\pi/2),\ 0,\ \cos(t\pi/2)). At t=12t = \tfrac12: x1/2=(0.7071,0,0.7071)x_{1/2} = (0.7071, 0, 0.7071), at latitude 45°. The straight-line midpoint (0.5,0,0.5)(0.5, 0, 0.5) has norm 0.70710.7071: on the Earth it lies 6371×(1−0.7071)≈18666371 \times (1 - 0.7071) \approx 1866 km below the surface.
    4. Velocity by differentiation: x˙t=π2(cos⁡(tπ/2), 0, −sin⁡(tπ/2))\dot x_t = \tfrac{\pi}{2}(\cos(t\pi/2),\ 0,\ -\sin(t\pi/2)), so x˙1/2=(1.1107,0,−1.1107)\dot x_{1/2} = (1.1107, 0, -1.1107), with speed π/2\pi/2 at every tt.
    5. Check Theorem 2 at t=12t = \tfrac12. The distance left is d(x1/2,x1)=arccos⁡(0.7071)=π/4d(x_{1/2}, x_1) = \arccos(0.7071) = \pi/4. The tangent part of x1x_1 at x1/2x_{1/2} is x1−0.7071 x1/2=(0.5,0,−0.5)x_1 - 0.7071\,x_{1/2} = (0.5, 0, -0.5), with unit vector (0.7071,0,−0.7071)(0.7071, 0, -0.7071). So log⁡x1/2(x1)=π4(0.7071,0,−0.7071)=(0.5554,0,−0.5554)\log_{x_{1/2}}(x_1) = \tfrac{\pi}{4}(0.7071, 0, -0.7071) = (0.5554, 0, -0.5554). Dividing by 1−t=0.51 - t = 0.5 gives (1.1107,0,−1.1107)(1.1107, 0, -1.1107), as in step 4.

    (b) One step of each sampler from x0x_0, with the velocity u0=(π/2,0,0)u_0 = (\pi/2, 0, 0).

    1. Plain Euler: (1.5708,0,1)(1.5708, 0, 1), of norm 1+π2/4=1.8621\sqrt{1 + \pi^2/4} = 1.8621: far off the sphere.
    2. Projected Euler: (1.5708,0,1)/1.8621=(0.8436,0,0.5370)(1.5708, 0, 1)/1.8621 = (0.8436, 0, 0.5370). It moved by the angle arctan⁡(π/2)=1.0039\arctan(\pi/2) = 1.0039 (57.5°), so it is still π/2−1.0039=0.5669\pi/2 - 1.0039 = 0.5669 (32.5°) away from x1x_1.
    3. Exponential map: cos⁡(π/2) x0+sin⁡(π/2) (1,0,0)=x1\cos(\pi/2)\,x_0 + \sin(\pi/2)\,(1, 0, 0) = x_1, exactly.

    With NN steps along the conditional field (section 5): plain Euler ends at norms 1.86211.8621, 1.56071.5607, 1.25561.2556, 1.12831.1283 for N=1,2,5,10N = 1, 2, 5, 10; projected Euler ends 0.56690.5669, 0.16940.1694, 0.01180.0118, 0.00140.0014 away from x1x_1; the exponential map is exact for every NN.

    (c) Near the antipode. x=(0,0,1)x = (0, 0, 1) and y=(±10−8,0,−1)y = (\pm 10^{-8}, 0, -1) (normalized). The two points yy are 2×10−82 \times 10^{-8} apart, but log⁡x(y)=(±π,0,0)\log_x(y) = (\pm\pi, 0, 0): opposite directions. A target that depends on log⁡\log is discontinuous there.

    5. In code #

    SciPy's rotations and geometric_slerp check the formulas of section 3:

    import numpy as np
    from scipy.spatial import geometric_slerp
    from scipy.spatial.transform import Rotation
    
    def exp_map(x, v):
        n = np.linalg.norm(v)
        return x if n == 0 else np.cos(n) * x + np.sin(n) * v / n
    
    def log_map(x, y):
        u = y - (x @ y) * x                                   # P_x y, the tangent part of y
        return np.arccos(np.clip(x @ y, -1, 1)) * u / np.linalg.norm(u)
    
    rng = np.random.default_rng(0)
    x, y = rng.standard_normal((2, 3))
    x, y = x / np.linalg.norm(x), y / np.linalg.norm(y)
    v = log_map(x, y)
    print(abs(x @ v), np.linalg.norm(exp_map(x, v) - y))      # 2.8e-17 1.6e-16: tangent, and exp(log y) = y
    print(np.linalg.norm(v), np.arccos(x @ y))                # 0.7616 0.7616: |log_x y| = geodesic distance
    
    # exp_x(w) is a rotation of x by the angle |w| about the axis x × w / |w|: rotvec = x × w.
    w = np.cross(x, rng.standard_normal(3))
    w = 2.5 * w / np.linalg.norm(w)                           # a tangent vector at x with |w| = 2.5 < pi
    print(np.linalg.norm(exp_map(x, w) - Rotation.from_rotvec(np.cross(x, w)).apply(x)),
          np.linalg.norm(log_map(x, exp_map(x, w)) - w))      # 1.6e-16 4.4e-16: log(exp w) = w for |w| < pi
    
    # The geodesic path exp_x(t log_x y) is SciPy's spherical interpolation (slerp).
    t = np.linspace(0, 1, 7)
    path = np.array([exp_map(x, s * v) for s in t])
    print(np.abs(path - geometric_slerp(x, y, t)).max())       # 2.5e-16
    
    # Its velocity, by central differences, equals log_{x_t}(y) / (1 - t), with constant speed d(x, y).
    s, h = 0.3, 1e-6
    xt = exp_map(x, s * v)
    vel = (exp_map(x, (s + h) * v) - exp_map(x, (s - h) * v)) / (2 * h)
    print(np.linalg.norm(vel - log_map(xt, y) / (1 - s)), np.linalg.norm(vel) - np.arccos(x @ y))  # 5.8e-11 -4.3e-11
    

    Then (same session) the three samplers on the conditional field of worked example (b), and the antipode of (c):

    x0, x1 = np.array([0.0, 0.0, 1.0]), np.array([1.0, 0.0, 0.0])
    u = lambda x, t: log_map(x, x1) / (1 - t)
    angle = lambda z: np.arccos(np.clip(z @ x1 / np.linalg.norm(z), -1, 1))   # angle between z and x1
    for N in (1, 2, 5, 10):
        h = 1 / N
        xe, xp, xr = x0.copy(), x0.copy(), x0.copy()
        for k in range(N):
            t = k * h
            xe = xe + h * u(xe / np.linalg.norm(xe), t)         # plain Euler (field read at the radial projection)
            xp = xp + h * u(xp, t); xp /= np.linalg.norm(xp)    # projected Euler (a retraction)
            xr = exp_map(xr, h * u(xr, t))                      # exponential-map Euler
        print(N, round(np.linalg.norm(xe), 4), round(angle(xp), 4), round(angle(xr), 12))
    # N  |x| plain Euler  angle to x1 (projected)  angle to x1 (exp map)
    # 1  1.8621           0.5669                   0.0
    # 2  1.5607           0.1694                   0.0
    # 5  1.2556           0.0118                   0.0
    # 10 1.1283           0.0014                   0.0
    
    x = np.array([0.0, 0.0, 1.0])
    for e in (1e-8, -1e-8):
        y = np.array([e, 0.0, -1.0]); y /= np.linalg.norm(y)
        print(log_map(x, y).round(4))                          # [ 3.1416 0. 0.], then [-3.1416 0. 0.]
    

    6. Where it shows up #

    • Earth-science event data. Chen and Lipman's sphere benchmarks are catalogues of volcanic eruptions, earthquakes, floods and fires (compiled by Mathieu and Nickel, 2020, from NOAA, the Dartmouth Flood Observatory and NASA fire data). They report test log-likelihoods for each.
    • Seismology and weather. Epicentre densities for seismic hazard; cyclone genesis points and lightning on S2S^2; wind directions on the circle; focal mechanisms (strike, dip and rake) as rotations in SO(3)SO(3), up to the symmetries of the double couple.
    • Proteins and robotics. Flow matching on SE(3)SE(3) and SO(3)SO(3) (for example FrameFlow and FoldFlow) generates protein backbones as rigid frames; Chen and Lipman also model protein dihedral angles on tori.
    • Software. Meta's open-source flow_matching library has a Sphere manifold (expmap, logmap, projx, proju), a geodesic probability path and a RiemannianODESolver, with a sphere example notebook.

    7. Common mistakes #

    • A velocity model that is not tangent. Without PxP_x, normal components push samples off the sphere. Project the output, and project the input point too if it may have drifted.
    • Plain Euler steps. They leave the sphere at every step; use the exponential map or project.
    • arccos without clipping. Round-off can give x⋅y=1.0000000000000002x \cdot y = 1.0000000000000002 (the next float above 1), and arccos returns NaN. Clip to [−1,1][-1, 1].
    • Dividing by ∥u∥≈0\lVert u\rVert \approx 0. For yy close to xx, log⁡x(y)≈Px(y−x)\log_x(y) \approx P_x(y - x); switch to it below a small threshold.
    • A "uniform" source made from uniform latitude and longitude. The area element is cos⁡(latitude) d(latitude) d(longitude)\cos(\text{latitude})\,d(\text{latitude})\,d(\text{longitude}), so uniform latitudes pile up mass near the poles. Normalize a standard 3D Gaussian instead: its density depends only on ∥z∥\lVert z\rVert, so its direction is uniform.
    • Different base points. Chen and Lipman write xt=exp⁡x1((1−t)log⁡x1x0)x_t = \exp_{x_1}\big((1 - t)\log_{x_1}x_0\big); other code computes exp⁡x0(tlog⁡x0x1)\exp_{x_0}(t\log_{x_0}x_1). Both are the same great-circle path when the geodesic is unique.
    • Fields versus points. SFNO (lesson 09) maps fields on the sphere to fields; Riemannian flow matching models where points fall on the sphere.

    8. Check yourself #

    1. Compute d(x,y)d(x, y) and log⁡x(y)\log_x(y) for x=(0,0,1)x = (0, 0, 1) and y=(0,3/2,1/2)y = (0, \sqrt3/2, 1/2). How far apart are the two points on the Earth?
      Answerx⋅y=1/2x \cdot y = 1/2, so ω=π/3≈1.0472\omega = \pi/3 \approx 1.0472. u=y−12x=(0,3/2,0)u = y - \tfrac12 x = (0, \sqrt3/2, 0), with ∥u∥=sin⁡ω\lVert u\rVert = \sin\omega. log⁡x(y)=(0,π/3,0)≈(0,1.0472,0)\log_x(y) = (0, \pi/3, 0) \approx (0, 1.0472, 0). On the Earth: 6371×1.0472≈66726371 \times 1.0472 \approx 6672 km.
    2. A projected Euler step and an exponential-map step use the same tangent step of length s=0.1s = 0.1. By how much do the angles they move differ?
      AnswerThe projected step moves arctan⁡(0.1)=0.099669\arctan(0.1) = 0.099669, the exponential map 0.10.1: a difference of 3.3×10−4≈s3/33.3 \times 10^{-4} \approx s^3/3.
    3. Show that RCFM with the geodesic path is the CFM of lesson 14 when the manifold is Rd\R^d.
      AnswerIn Rd\R^d, exp⁡x(v)=x+v\exp_x(v) = x + v and log⁡x(y)=y−x\log_x(y) = y - x. So xt=x0+t(x1−x0)x_t = x_0 + t(x_1 - x_0), and the target log⁡xt(x1)/(1−t)=(x1−xt)/(1−t)=x1−x0\log_{x_t}(x_1)/(1 - t) = (x_1 - x_t)/(1 - t) = x_1 - x_0. The tangent projection is the identity.
    4. Why is training simulation-free on the sphere but not on a general triangle mesh?
      AnswerOn the sphere, exp⁡\exp and log⁡\log have closed forms, so xtx_t and the target are computed directly. On a mesh, geodesics have no closed form; Chen and Lipman use a spectral distance instead, and then xtx_t must be found by solving the ODE of the conditional velocity.
    5. Your trained model's samples have ∣∥x∥−1∣≈10−2\big\lvert\lVert x\rVert - 1\big\rvert \approx 10^{-2}. Name two likely causes.
      AnswerPlain Euler steps (each adds h2∥v∥2h^2\lVert v\rVert^2 to ∥x∥2\lVert x\rVert^2), or a velocity network whose output is not projected onto the tangent plane. With exp-map or projected steps and a tangent model, the error is at round-off level.
    6. Is log⁡x(exp⁡xv)=v\log_x(\exp_x v) = v for ∥v∥=4\lVert v\rVert = 4? Explain with a picture.
      AnswerNo. Walking 4 > π along the great circle passes the antipode −x-x; the end point is closer to xx the other way round, at distance 2π−4≈2.282\pi - 4 \approx 2.28. The log gives that shorter way: a vector of length 2π−42\pi - 4 pointing opposite to vv.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. What is the projection onto the tangent plane of S2S^2 at xx?

    2. What is the geodesic distance between unit vectors xx and yy on S2S^2?

    3. Write the exponential map of the sphere.

    4. Write the logarithm map of the sphere.

    5. Why is log⁡x\log_x undefined at y=−xy = -x?

    6. What is the velocity of the geodesic path xt=exp⁡x0(tlog⁡x0x1)x_t = \exp_{x_0}(t\log_{x_0}x_1)?

    7. Write the Riemannian CFM loss.

    8. One plain Euler step x+hvx + hv with tangent vv: what is ∥x+hv∥2\lVert x + hv\rVert^2?

    9. Why use a retraction such as (x+hv)/∥x+hv∥(x + hv)/\lVert x + hv\rVert when sampling?

    Open the review →

    9. Interview angle #

    • "How would you build a generative model of earthquake epicentres?" Convert latitude and longitude to unit vectors, use a uniform source on S2S^2, the geodesic path, a tangent-projected MLP velocity, the RCFM loss, and an exponential-map or projected ODE solver. Evaluate the test log-likelihood, as Chen and Lipman do.
    • "Why not just normalize the output of a Euclidean model?" The training paths and the model live in R3\R^3 (through the Earth), and the normalization is not part of the learned flow. It can work, but it wastes capacity, and the density on the sphere is not modelled directly.
    • "Diffusion or flow matching on manifolds?" Manifold diffusion models need simulation during training and approximations of the score on the manifold; Riemannian flow matching is simulation-free on simple manifolds such as the sphere.
    • "What goes wrong numerically?" Points leaving the manifold, NaNs from arccos, division by ∥u∥≈0\lVert u\rVert \approx 0 near xx, and unstable directions near the antipode.
    • "What is the analogue of the OT straight path on a manifold?" The geodesic interpolation; in Rd\R^d it is the conditional OT path of lesson 14.

    10. Practice #

    The notebook for this lesson runs in Colab or in the browser. It has five exercises, each with a check cell and a folded solution:

    1. The exponential and logarithm maps of S2S^2 and the round-trip test.
    2. The geodesic path equals slerp (scipy.spatial.geometric_slerp).
    3. The velocity of the geodesic path by central differences equals log⁡xt(x1)/(1−t)\log_{x_t}(x_1)/(1 - t).
    4. Plain, projected and exponential-map Euler on the conditional velocity.
    5. A data set of three clusters on the sphere: the exact marginal velocity (a weighted average of conditional velocities) and sampling from the uniform distribution to the clusters.

    11. Watch and read #

    Flow matching and its generalizations by its first author; compare with Theorem 3 of this lesson.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A lecture on generative models on Riemannian manifolds, the general setting of this lesson.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The Euclidean flow matching of lesson 14, which this lesson moves to the sphere.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play