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

Add a custom optimizer that implements AdamW with proper magnitude computation for complex tensors #420

Merged
merged 15 commits into from
Aug 26, 2024

Conversation

dhpitt
Copy link
Member

@dhpitt dhpitt commented Aug 20, 2024

torch.optim optimizers view all complex parameters as real before computing update steps. Even after the 2021 update to support complex tensors, the magnitudes computed for momentum are thrown off by computing grad * grad.conj() for a 2-tensor real stack, which is equivalent to squaring the real and imaginary components separately. This is different than an actual multiplication of grad * grad.conj() for complex numbers:

>>> x = torch.randn((2,2), dtype=torch.cfloat)

>>> x

tensor([[-1.1954+0.7736j, -0.3455+0.1481j],
        [-1.2845+0.6057j,  0.1566+0.3581j]])

>>> y = torch.view_as_real(x)

>>> y

tensor([[[-1.1954,  0.7736],
         [-0.3455,  0.1481]],

        [[-1.2845,  0.6057],
         [ 0.1566,  0.3581]]])

>>> torch.isclose(y**2, y * y.conj()).all()

tensor(True)

>>> torch.isclose(x**2, x * x.conj()).all()

tensor(False)

@dhpitt dhpitt marked this pull request as ready for review August 20, 2024 16:54
@dhpitt dhpitt merged commit 3c95d8e into neuraloperator:main Aug 26, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant