Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quaternion multiplication wrong, because of Tensor.cross #2822

Open
yzhhr opened this issue Mar 3, 2024 · 1 comment
Open

Quaternion multiplication wrong, because of Tensor.cross #2822

yzhhr opened this issue Mar 3, 2024 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@yzhhr
Copy link

yzhhr commented Mar 3, 2024

Describe the bug

The expression c should give the same result but it actually goes wrong.

import torch
torch.manual_seed(42)
import kornia
from kornia.geometry.quaternion import Quaternion
x = Quaternion.random(3)
a = x * x
b = x[0] * x[0]
c = x[[0,0,0]] * x
d = x[[0,1,2]] * x
print(a[0], b, c[0], d[0])

The code gives

Parameter containing:
tensor([-0.9849, -0.0576, -0.1629,  0.0067], requires_grad=True) Parameter containing:
tensor([-0.9849, -0.0576, -0.1629,  0.0067], requires_grad=True) Parameter containing:
tensor([-0.9849, -0.1930,  0.4901, -0.0056], requires_grad=True) Parameter containing:
tensor([-0.9849, -0.0576, -0.1629,  0.0067], requires_grad=True)

May affect more behaviors

For example the so3 and se3 lie group actions also depend on quaternions.

Tracing the bug

The bug is caused by a call to Tensor.cross, which finds the first dimension of size=3 as vec3. The code can be found in Quaternion.__mul__. Replacing the call with a more stable torch.linalg.cross can fix the issue, according to my experiment.

It is weird that only expression c actually triggers the bug. PyTorch actually gives a warning of Tensor.cross deprecated but no one seems to be caring?

Can I possibly help fix it in a PR?

One more question

Is Quaternion class designed to be not supporting broadcasting? Why those modules default to be requires_grad=True even when initializers are requires_grad=False?

Reproduction steps

Just run the sample code.

Expected behavior

All four expressions should give the same result.

Environment

kornia 0.7.2.dev0, torch 2.2.1

Additional context

No response

@yzhhr yzhhr added the help wanted Extra attention is needed label Mar 3, 2024
@cjpurackal
Copy link
Member

Good catch, please go ahead with the PR and it would be great if you can update the tests as well!

@edgarriba edgarriba linked a pull request Mar 7, 2024 that will close this issue
7 tasks
@edgarriba edgarriba removed a link to a pull request Mar 7, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants