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

🗻 autograd: geometry group #1768

Merged
merged 1 commit into from
Jun 19, 2024
Merged

🗻 autograd: geometry group #1768

merged 1 commit into from
Jun 19, 2024

Conversation

tylerflex
Copy link
Collaborator

@tylerflex tylerflex commented Jun 17, 2024

@tylerflex tylerflex added 2.8 will go into version 2.8.* and removed 2.8 will go into version 2.8.* labels Jun 18, 2024
Copy link
Contributor

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

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

Looks good!

Untitled.ipynb Outdated Show resolved Hide resolved
tidy3d/web/api/autograd/autograd.py Show resolved Hide resolved
@tylerflex
Copy link
Collaborator Author

hey @yaugenst-flex when you get a chance, could you look again with the new added changes to add a components/autograd/ directory and also passing things in the DerivativeInfo. Thanks

@tylerflex tylerflex mentioned this pull request Jun 19, 2024
95 tasks
@tylerflex tylerflex changed the title autograd geometry group autograd: geometry group Jun 19, 2024
@tylerflex tylerflex changed the title autograd: geometry group 🗻 autograd: geometry group Jun 19, 2024
Copy link
Contributor

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

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

Nice I think the DerivativeInfo change is great. Also opens up the possibility for some derivative-specific helpers down the line.
Having a utils.py and a derivative_utils.py feels a bit weird to me, but not sure if it needs changing. I guess we'll merge this and I'll add #1769's interpn to a functions.py here?

)
from .utils import get_static

# from .derivative_utils import DerivativeInfo, integrate_within_bounds
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why this is commented out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because it causes circular imports when this directory is imported into eg base.py. I'll remove the comment though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually i need to double check this. It might be fixed now that i use dict of scalar field data array

# class passed to gradient computations
# defined here to make things more compact, extendable, and better documented

# we do this because importing these creates circular imports
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is fine for now but I think we should figure out how to do this more robustly... either reorganizing the types so that this doesn't happen or possibly use forward references.
Or maybe the best way would actually to from __future__ import annotations 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The issue is that MonitorData such as FieldData can't be imported into eg geometry and medium, where these derivates are computed.

One option might be to extract the derivative computation to somewhere in web/api/autograd instead of being methods of the classes. However, i do kind of like having them defined in the classes as it keeps things organized.

I think annotations will not really help here but could be wrong. Also I'm wary about messing with forward refs unless it's necessary.

Copy link
Contributor

@yaugenst-flex yaugenst-flex Jun 19, 2024

Choose a reason for hiding this comment

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

No this should be the exact use case for from __future__ import annotations then 😄 Postponed evaluation of annotations, i.e. you don't need to import the type you're using as a type hint. And it's essentially a forward ref under the hood.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea maybe. I need to read up on forward refs a bit more. Seems like a code smell to me though?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Practically speaking there was no issue with just passing the field data as generic dicts of data arrays.

from autograd.tracer import getval


def get_static(x: typing.Any) -> typing.Any:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really necessary? Why don't we just use getval directly? If it's because of naming, we could also from autograd.tracer import getval as get_static here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea so originally this had some special handling for other types before i learned about getval. I'd say for now let's leave it in case we need to modify things? But probably in the future we can just use getval everywhere we use getstatic

@tylerflex
Copy link
Collaborator Author

Nice I think the DerivativeInfo change is great. Also opens up the possibility for some derivative-specific helpers down the line.

Yea it cleans things up for sure and now i feel less resistance to adding many more options eg for resolution for the eventual cylinder integrals or other things.

Having a utils.py and a derivative_utils.py feels a bit weird to me, but not sure if it needs changing. I guess we'll merge this and I'll add #1769's interpn to a functions.py here?

Yea it seems a bit weird to me too. Wasn't sure what to rename it to. If you add interpn maybe we can move these to functions.py. Or maybe there's a more descriptive name? I'm fine with it for now

@tylerflex tylerflex force-pushed the tyler/autograd_/geo_group branch 2 times, most recently from 6494018 to 4bdd04f Compare June 19, 2024 14:44
@tylerflex tylerflex merged commit 3ab33eb into develop Jun 19, 2024
15 checks passed
@tylerflex tylerflex deleted the tyler/autograd_/geo_group branch June 19, 2024 14:46
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