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 TensorTree type #7

Merged
merged 1 commit into from
Jan 22, 2024
Merged

Add TensorTree type #7

merged 1 commit into from
Jan 22, 2024

Conversation

SamDuffield
Copy link
Contributor

@SamDuffield SamDuffield commented Jan 22, 2024

Added TensorTree type. Don't think there are other custom types we'd like to add currently? But I think there will be in future.

from optree.typing import PyTreeTypeVar
from torch import Tensor

TensorTree: TypeAlias = PyTreeTypeVar("TensorTree", Tensor)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So PyTreeTypeVar("custom_name", Type) defines a type custom_name of pytree where the leaf nodes all have type Type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah see optree.PyTreeTypeVar

Although looking at it now PyTree[torch.Tensor] is perhaps cleaner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think either is fine :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep like this for now, this is what torchopt does 😄

Copy link

@dmitrisaberi dmitrisaberi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be a stupid question but why do we care so much about generalizing to tree-like structures? what are some crucial examples where parameters are not just dicts? (otherwise LGTM)

@SamDuffield
Copy link
Contributor Author

this may be a stupid question but why do we care so much about generalizing to tree-like structures? what are some crucial examples where parameters are not just dicts? (otherwise LGTM)

The object to be optimised could just be a tensor (useful for testing and more traditional MCMC).

Also can support lists of dicts (which I think is common in pytorch with so-called "parameter groups") https://pytorch.org/docs/stable/optim.html

@dmitrisaberi
Copy link

this may be a stupid question but why do we care so much about generalizing to tree-like structures? what are some crucial examples where parameters are not just dicts? (otherwise LGTM)

The object to be optimised could just be a tensor (useful for testing and more traditional MCMC).

Also can support lists of dicts (which I think is common in pytorch with so-called "parameter groups") https://pytorch.org/docs/stable/optim.html

makes sense, thanks!

@SamDuffield SamDuffield merged commit ac07f37 into main Jan 22, 2024
2 checks passed
@SamDuffield SamDuffield deleted the types branch January 22, 2024 17:33
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.

2 participants