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

refactor gradient calculations to use DerivativeSurfaceMesh #2056

Merged
merged 2 commits into from
Dec 12, 2024

Conversation

tylerflex
Copy link
Collaborator

Part 1 of an effort to generalize the VJP calculations so the Geometry and Medium just need to construct a single DerivativeSurfaceMesh for every field. The DerivativeInfo then has a method that computes derivatives given a DerivativeSurfaceMesh.

@tylerflex tylerflex marked this pull request as draft November 4, 2024 19:33
@tylerflex tylerflex force-pushed the tyler/autograd_/polyslab_refactor branch 2 times, most recently from c4a9272 to e721e9b Compare November 12, 2024 21:12
@tylerflex tylerflex force-pushed the tyler/autograd_/polyslab_refactor branch 4 times, most recently from ad260c1 to a644b17 Compare November 25, 2024 21:43
@tylerflex tylerflex changed the base branch from develop to pre/2.8 November 25, 2024 21:44
@tylerflex tylerflex marked this pull request as ready for review November 25, 2024 21:44
@tylerflex tylerflex added 2.8 will go into version 2.8.* rc1 1st pre-release labels Nov 25, 2024
@tylerflex tylerflex mentioned this pull request Nov 26, 2024
100 tasks
@tylerflex
Copy link
Collaborator Author

Let's also get this one in. Looks like I have a good amount of merge conflicts to sort out first, but I'll work on that today and re-test

@tylerflex tylerflex force-pushed the tyler/autograd_/polyslab_refactor branch from 6778774 to a9cb96e Compare December 4, 2024 15:23
@tylerflex
Copy link
Collaborator Author

did a bit of testing of this and it seems like it still works. going to leave it unsquashed for now but should be ready to go after reviews @momchil-flex @yaugenst-flex

@tylerflex
Copy link
Collaborator Author

I just noticed an issue with the epsilon grabbing from the other PR after the rebase, so don't merge this yet as I'll fix it tomorrow morning

@tylerflex
Copy link
Collaborator Author

alright fixed it, turned out when there were inf in the PolySlab.slab_bounds or Cylinder.length, some weird things would happen after the rebase. These are handled properly now.

Copy link
Collaborator

@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.

Really nice to have this 🎉
Only some minor comments. Biggest issue for me is, this is basically a merge from develop right? I think we should do that separately, there are a lot of unrelated changes in here.

tidy3d/components/autograd/derivative_utils.py Outdated Show resolved Hide resolved
tidy3d/components/autograd/derivative_utils.py Outdated Show resolved Hide resolved
tidy3d/components/autograd/derivative_utils.py Outdated Show resolved Hide resolved
tidy3d/components/data/sim_data.py Outdated Show resolved Hide resolved
tidy3d/components/geometry/base.py Outdated Show resolved Hide resolved
@momchil-flex
Copy link
Collaborator

Yeah @tylerflex I think you need to drop the first 6 commits (then maybe clean up the rest?)

@tylerflex tylerflex force-pushed the tyler/autograd_/polyslab_refactor branch from f3d8e79 to 3cb0cfd Compare December 9, 2024 17:24
@tylerflex
Copy link
Collaborator Author

ok I cleaned up the commits into just 2, addressed Yannick's comments, but now ruff is complaining (but not locally, so that's a bit weird)

@tylerflex tylerflex force-pushed the tyler/autograd_/polyslab_refactor branch from 3cb0cfd to 144ab3e Compare December 9, 2024 17:42
@yaugenst-flex yaugenst-flex self-requested a review December 9, 2024 18:02
tidy3d/components/autograd/derivative_utils.py Outdated Show resolved Hide resolved
@@ -248,34 +248,25 @@ def get_monitor_name(index: int, data_type: str) -> str:
return monitor_name_map[data_type]

def make_adjoint_monitors(
self, freqs: list[float], index: int
self, freqs: list[float], index: int, field_keys: list[str]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Whenever changes are made to the frontend autograd components we need to be careful about the backend too. Seems like this needs to be updated and I'm not sure how?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, right. I think the fix is to pass the sim_fields_keys into the make_adjoint_monitors. Since you have the optimizer, that would mean getting

sim = optimizer.design.simulation
traced_fields_sim = setup_run(simulation=sim)
sim_fields_keys = list(traced_fields_sim.keys())

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think setup_run will work if this is not called inside a grad call?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not sure, guess you could just do


    return simulation.strip_traced_fields(
        include_untraced_data_arrays=False, starting_path=("structures",)
    )

which is what setup run does.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But the point is that there are no traced fields since it's not a simulation called in grad no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Basically what we're trying to do there is get the gradient monitors from the static definition of the InverseDesign object.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah ok. yea I guess we dont know. but we can effectively "fake" it then, because we know it's a Box with a CustomMedium and we need the permittivity data.

So I think one approach could just be to pass

field_keys=(f'structures/{(len(sim.structures) - 1}/medium/permittivity', )

into the make_adjoint_monitors call. Or you can just make the monitors manually since again we know it's just a box so it needs a similarly sized field and permittivity monitor

Copy link
Collaborator

Choose a reason for hiding this comment

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

Using the first approach, works for now, obviously should be dealt with better in the future..

tidy3d/components/geometry/polyslab.py Outdated Show resolved Hide resolved
@tylerflex tylerflex force-pushed the tyler/autograd_/polyslab_refactor branch from 144ab3e to e582bbc Compare December 11, 2024 19:04
@momchil-flex momchil-flex merged commit 9cf8721 into pre/2.8 Dec 12, 2024
15 checks passed
@momchil-flex momchil-flex deleted the tyler/autograd_/polyslab_refactor branch December 12, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.8 will go into version 2.8.* rc1 1st pre-release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

autograd support for PolySlab.slab_bounds Adjoint PolySlab.slab_bounds support Generalize VJP calculation
4 participants