-
Notifications
You must be signed in to change notification settings - Fork 63
autograd compatible s-matrix calculation #2572
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
Conversation
4951e4c
to
44cd50b
Compare
NOTE: this doesn't cover the terminal component modeler, that will require some modifications inspired by this. I didnt want to mess with it in case I broke something. |
44cd50b
to
ed68d39
Compare
ed68d39
to
bfc34ba
Compare
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/plugins/smatrix/component_modelers/base.pyLines 256-265 256 def batch_data(self) -> BatchData:
257 """The :class:`.BatchData` associated with the simulations run for this component modeler."""
258
259 # NOTE: uses run_async because Batch is not differentiable.
! 260 batch = self.batch
! 261 run_async_kwargs = batch.dict(
262 exclude={
263 "type",
264 "path_dir",
265 "attrs", Lines 268-276 268 "num_workers",
269 "simulations",
270 }
271 )
! 272 return run_async(
273 batch.simulations,
274 **run_async_kwargs,
275 local_gradient=LOCAL_GRADIENT,
276 path_dir=self.path_dir, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
4 files reviewed, no comments
Edit PR Review Bot Settings | Greptile
9915820
to
69bbee5
Compare
4d65bb6
to
548953f
Compare
79ccc3a
to
9449fe4
Compare
a4d1737
to
4c6732a
Compare
Testing with terminal in my patch antenna optimization, and one other spot we need a change is on line 319/320 in terminal.py, to replace
with
I'm re-running the whole optimization currently, but so far it looks like it is working with this change! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to verify with my optimization that uses the terminal component modeler after locally adding the other _with_updated_data call. Initially, though, the optimization wasn't working and I realized it was because I need local_gradient=True on so that the gradient gets computed through my branch (for PEC changes). I'm not sure the best way to enable this, but I added another field to AbstractComponentModeler that was local_gradient with a default of False, and then when the run_async call gets made, I pass in local_gradient=self.local_gradient.
@groberts-flex thanks I'll revisit your other comments after my meetings this morning, but I added the initial fix in |
I think we could consider either adding |
i think it definitely should not be a model field but rather an argument to the run function. the default can be set via config soon. but it should not be part of the schema |
@yaugenst-flex unfortunately the way things are organized right now, it seems like it needs to be a model field unless we want to break backwards compatibility. Maybe in light of the RF changes to the TerminalComponentModeler, we could consider waiting, but here's my stab at it 47130cf Basically the |
3463a9f
to
81a39e4
Compare
5a6647f
to
ce3fe38
Compare
@yaugenst-flex want to take a final look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
ce3fe38
to
207d118
Compare
Objective functions involving s-matrix calculations using
tidy3d.plugins.smatrix.ComponentModeler
are now differentiable with autograd.Here is an example:
Greptile Summary
Added autograd compatibility to S-matrix calculations in
ComponentModeler
, enabling gradient-based optimization for photonic component design through automatic differentiation.plugins/smatrix/component_modelers/base.py
to useautograd.numpy
andrun_async
instead of non-differentiablebatch.run
plugins/smatrix/component_modelers/modal.py
to use mask-based indexing for S-matrix construction instead ofloc
indexingplugins/autograd/README.md
demonstrating gradient computation for power transmission objectives