geoopt icon indicating copy to clipboard operation
geoopt copied to clipboard

What needs to be done for TODOs in manifolds/stereographic?

Open lancejpollard opened this issue 2 years ago • 4 comments

I noticed in manifolds/stereographic/math.py there are a few TODOs:

# TODO: check numerical stability with Gregor's paper!!!
@torch.jit.script
def _mobius_coadd(x: torch.Tensor, y: torch.Tensor, k: torch.Tensor, dim: int = -1):
    # x2 = x.pow(2).sum(dim=dim, keepdim=True)
    # y2 = y.pow(2).sum(dim=dim, keepdim=True)
    # num = (1 + K * y2) * x + (1 + K * x2) * y
    # denom = 1 - K ** 2 * x2 * y2
    # avoid division by zero in this way
    # return num / denom.clamp_min(1e-15)
    #
    return _mobius_add(x, _gyration(x, -y, y, k=k, dim=dim), k, dim=dim)

# TODO: can we make this operation somehow safer by breaking up the
# TODO: scalar multiplication for K>0 when the argument to the
# TODO: tan function gets close to pi/2+k*pi for k in Z?
# TODO: one could use the scalar associative law
# TODO: s_1 (X) s_2 (X) x = (s_1*s_2) (X) x
# TODO: to implement a more stable Möbius scalar mult
def mobius_scalar_mul(r: torch.Tensor, x: torch.Tensor, *, k: torch.Tensor, dim=-1):

# TODO: check if this extends to gyrovector spaces for positive curvature
def mobius_pointwise_mul(w: torch.Tensor, x: torch.Tensor, *, k: torch.Tensor, dim=-1):

Could you explain what needs to be done for these? I am curious if I can help out here in some way. I am not a math expert but I am interested in using this functionality for hyperbolic spaces and hope I can help finish any outstanding pieces.

Thank you.

lancejpollard avatar Jan 24 '23 09:01 lancejpollard

Also noticed this:

Numerical stability is a pain in this model. It is strongly recommended to work in float64, so expect adventures in float32 (but this is not certain).

Are there any models where numerical stability isn't a pain? Also what exactly do you mean by a pain?

lancejpollard avatar Jan 24 '23 10:01 lancejpollard

The definitions of some operations in the poincare model (like the distance function) make them very susceptible to producing NaNs (due to possible divisions by 0 for example). When you combine that with the lower precision of float32 which tends to round operation outputs, your model becomes much more unstable and is likely to throw errors. Float64 alleviates this by lessening the amount of rounding that you do and allows you to store much bigger numbers.

There are other models like the hyperboloid/Lorentz model which have more stable definitions but from what I've seen they also don't have the operation flexibility the poincare model has (defined mobius addition...)

There's the paper: https://arxiv.org/abs/2211.00181 That sums up the optimization and numerical stability differences. It's also a bit interesting because it shows that despite the hyperboloid being less likely to cause crash issues, it does have a smaller representation capacity. Numbers that don't cause you to crash can still be inaccurate enough to no longer be representative of the actual hyperbolic model.

inboxedshoe avatar Jan 24 '23 10:01 inboxedshoe

What if I just want to render like the HyperRogue game tessellations, will I run into rounding errors in either system? Or is this just like calculating values near infinity to get ultra precise for like scientific simulation/research sorta stuff?

lancejpollard avatar Jan 24 '23 17:01 lancejpollard

Oh I forgot before closing to get what needs to be done for the TODOs :)

lancejpollard avatar Jan 24 '23 18:01 lancejpollard