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

AttributeError: 'OrthoGrad' object has no attribute 'state' #326

Closed
Vectorrent opened this issue Jan 20, 2025 · 1 comment
Closed

AttributeError: 'OrthoGrad' object has no attribute 'state' #326

Vectorrent opened this issue Jan 20, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Vectorrent
Copy link
Contributor

Describe the bug

I am trying to test the new OrthoGrad wrapper, but it fails with the following error, when I try to use it with AdamW and Lion (and presumably others). It appears that Pytorch Lightning expects an optimizer.state attribute to exist on the OrthoGrad optimizer; however, it does not, and thus crashes.

I tested Lookahead, and it's working fine. OrthoGrad is the only wrapper giving me trouble.

To Reproduce

  • OS : Arch Linux
  • PyTorch version : 2.5.1
  • PyTorch-Lightning : 2.5.0.post0
  • Python version : 3.13.1
  • pytorch-optimizer version : 3.3.4

Log

Traceback (most recent call last):
  File "/home/crow/repos/praxis/run.py", line 1113, in <module>
    trainer.fit(
    ~~~~~~~~~~~^
        train_model,
        ^^^^^^^^^^^^
        datamodule,
        ^^^^^^^^^^^
        ckpt_path=ckpt_path,
        ^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/crow/repos/praxis/.venv/lib/python3.13/site-packages/lightning/pytorch/trainer/trainer.py", line 539, in fit
    call._call_and_handle_interrupt(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self, self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/crow/repos/praxis/.venv/lib/python3.13/site-packages/lightning/pytorch/trainer/call.py", line 68, in _call_and_handle_interrupt
    trainer._teardown()
    ~~~~~~~~~~~~~~~~~^^
  File "/home/crow/repos/praxis/.venv/lib/python3.13/site-packages/lightning/pytorch/trainer/trainer.py", line 1005, in _teardown
    self.strategy.teardown()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/crow/repos/praxis/.venv/lib/python3.13/site-packages/lightning/pytorch/strategies/strategy.py", line 532, in teardown
    _optimizers_to_device(self.optimizers, torch.device("cpu"))
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/crow/repos/praxis/.venv/lib/python3.13/site-packages/lightning/fabric/utilities/optimizer.py", line 27, in _optimizers_to_device
    _optimizer_to_device(opt, device)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/home/crow/repos/praxis/.venv/lib/python3.13/site-packages/lightning/fabric/utilities/optimizer.py", line 32, in _optimizer_to_device
    for p, v in optimizer.state.items():
                ^^^^^^^^^^^^^^^
AttributeError: 'OrthoGrad' object has no attribute 'state'

Expected behavior

I would expect the wrapper to behave like Lookahead does: as a wrapper around an Optimizer instance.

@Vectorrent Vectorrent added the bug Something isn't working label Jan 20, 2025
@Vectorrent
Copy link
Contributor Author

This PR should address the bug. I've tested locally, and it works.
#327

@kozistr kozistr closed this as completed Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants