Skip to content

refactor(notebooks): use internal adam and optimize helper - batch 4#513

Merged
marcorudolphflex merged 2 commits into
developfrom
use-internal-adam-and-optimize-helper-batch-4-nobrainer
Jun 11, 2026
Merged

refactor(notebooks): use internal adam and optimize helper - batch 4#513
marcorudolphflex merged 2 commits into
developfrom
use-internal-adam-and-optimize-helper-batch-4-nobrainer

Conversation

@marcorudolphflex

@marcorudolphflex marcorudolphflex commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

follow-up from #463

Replaces the optax Adam with our Adam and makes use of the optimize helper where easily possible. This is the fourth batch, reduced to the 9 notebooks from #465 where develop still had real optax usage. Metalens.ipynb, Autograd27Smatrix.ipynb, and Autograd25WaveguideCrossing.ipynb are intentionally left out for separate discussion.

Results are roughly unchanged overall. Relevant comparison notes copied from #465 are below.

Percent differences below use (branch - develop) / develop. For max objectives, positive is better; for min objectives/costs, negative is better.

Status Notebook Direction develop branch diff diff % Note
TidyFab0GC max 3.0199e-01 5.1519e-01 +2.1320e-01 +70.60% objective, unexpectedly better
Autograd19ApodizedCoupler max 4.799e-01 4.801e-01 +2.0e-04 +0.04% objective; coupling efficiency develop -1.98 dB vs. branch -1.99 dB
Autograd7Metalens max 2.2378e+01 2.3344e+01 +9.660e-01 +4.32% objective, roughly unchanged
Autograd26DiffractiveBeamSplitter max 8.792e-01 8.790e-01 -2.0e-04 -0.02% loss value; cost min changes by +0.02%; final efficiency 0.91 to 0.89
Autograd28FiberLens max 4.0488e-02 4.0460e-02 -2.8e-05 -0.07% final flux, plot/result nearly unchanged
BilayerSiNSiGC max 8.134e-01 8.139e-01 +5.0e-04 +0.06% objective, effectively unchanged
RFAutograd1RectangularPatchAntenna max 20.812315 20.717626 -9.4689e-02 -0.45% final nonzero FOM, roughly unchanged
Autograd13Metasurface min 1.194e-01 1.185e-01 -9.0e-04 -0.75% final loss, roughly unchanged
Autograd23FabricationAwareInvdes max 0.09930831775707324 0.09930831775707324 0 0.00% initial objective check; no credits for full rerun

TidyFab0GC.ipynb

Objective (max.): develop 3.0199e-01 vs. branch 5.1519e-01 (+70.60%)

Unexpectedly better. This may be from client/solver changes rather than the optimizer refactor.

Autograd19ApodizedCoupler.ipynb

Objective (max.): develop 4.799e-01 vs. branch 4.801e-01 (+0.04%)

Maximum coupling efficiency: develop -1.98 dB vs. branch -1.99 dB

Rerun is back in line with develop.

Autograd7Metalens.ipynb

Objective (max.): develop 2.2378e+01 vs. branch 2.3344e+01 (+4.32%)

Plot/result roughly unchanged.

Autograd26DiffractiveBeamSplitter.ipynb

develop:

Loss value (maximize): 0.8792
  Cost (minimize): 9.2696e-01
  Fab penalty: 9.9816e-01
  Efficiency: 0.9999
image

branch:

Loss value (maximize): 0.8790
  Cost (minimize): 9.2712e-01
  Fab penalty: 9.9816e-01
  Efficiency: 0.9999
image

Later final-efficiency output: develop 0.91 vs. branch 0.89.

Autograd28FiberLens.ipynb

Objective / final flux (max.): develop 4.0488e-02 vs. branch 4.0460e-02 (-0.07%)

Plot/result nearly unchanged.

BilayerSiNSiGC.ipynb

Objective (max.): develop 8.134e-01 vs. branch 8.139e-01 (+0.06%)

Effectively unchanged.

RFAutograd1RectangularPatchAntenna.ipynb

Final nonzero FOM: develop 20.812315 vs. branch 20.717626 (-0.45%).

Optimization a little less smooth now, result differs a bit:

image image image image

Autograd13Metasurface.ipynb

Final loss (min.): develop 1.194e-01 vs. branch 1.185e-01 (-0.75%).

Autograd23FabricationAwareInvdes.ipynb

Did not rerun this notebook because I do not have credits for the prefab/fabrication-aware run path. No result shift is expected from this diff.


Note

Low Risk
Notebook-only optimizer wiring changes with no library API changes; numerical drift is minor except one flagged rerun outlier.

Overview
Fourth batch of inverse-design notebook cleanup: nine tutorials drop optax in favor of tidy3d.plugins.autograd adam, apply_updates, and optimize() where the loop is a straight min/max with bounds.

TidyFab0GC is refactored to call optimize(..., direction="max") with a step callback and min-feature bounds instead of hand-rolled Adam steps. Others (Autograd19ApodizedCoupler, Autograd13Metasurface, Autograd26DiffractiveBeamSplitter, etc.) keep explicit loops but swap optax.adam / optax.apply_updates for the internal Adam API; notebooks that still need custom iteration (e.g. beta ramping, live plots) are not forced onto optimize(). Metalens.ipynb, Autograd27Smatrix, and Autograd25WaveguideCrossing are out of scope for this batch.

Reruns vs. develop are mostly within ~1% on objectives; TidyFab0GC shows a large objective jump that may be environment/solver noise rather than the optimizer swap. Autograd23FabricationAwareInvdes was not fully rerun (prefab credits).

Reviewed by Cursor Bugbot for commit ec0d8ee. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Spell Check Report

TidyFab0GC.ipynb:

Cell 65, Line 10: 'pre-update'
  > # The callback records pre-update iterates, so param_history

Checked 9 notebook(s). Found spelling errors in 1 file(s).
Generated by GitHub Action run: https://github.com/flexcompute/tidy3d-notebooks/actions/runs/27344014223

@marcorudolphflex marcorudolphflex marked this pull request as ready for review June 5, 2026 13:12

@groberts-flex groberts-flex left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks @marcorudolphflex! overall looks great! I'm a little curious about the RF one where the optimization curve looks less smooth. I wonder if that's some other changes since the last time develop was run or the alternate Adam optimizer behaves slightly differently than optax. Either way, the final performance looks good and I don't think this is a blocker at all!

One small other thing that I came across while going through this with codex was that in TidyFab0GC.ipynb, the best_params are taken from the param_history but those are updated in the callback before the device is stepped. So we could either use the final parameters that are returned or fill in the final device and figure of merit into the param_history.

@marcorudolphflex

Copy link
Copy Markdown
Contributor Author

thanks @marcorudolphflex! overall looks great! I'm a little curious about the RF one where the optimization curve looks less smooth. I wonder if that's some other changes since the last time develop was run or the alternate Adam optimizer behaves slightly differently than optax. Either way, the final performance looks good and I don't think this is a blocker at all!

One small other thing that I came across while going through this with codex was that in TidyFab0GC.ipynb, the best_params are taken from the param_history but those are updated in the callback before the device is stepped. So we could either use the final parameters that are returned or fill in the final device and figure of merit into the param_history.

Thanks!
In the past, I reran many notebooks where I reverted optax changes to see if that was the cause and it never changed anything - which is why I skipped that here. Pretty sure some other changes caused that.

Good catch, went with the "final params" way here.

@groberts-flex

Copy link
Copy Markdown
Contributor

thanks @marcorudolphflex! overall looks great! I'm a little curious about the RF one where the optimization curve looks less smooth. I wonder if that's some other changes since the last time develop was run or the alternate Adam optimizer behaves slightly differently than optax. Either way, the final performance looks good and I don't think this is a blocker at all!
One small other thing that I came across while going through this with codex was that in TidyFab0GC.ipynb, the best_params are taken from the param_history but those are updated in the callback before the device is stepped. So we could either use the final parameters that are returned or fill in the final device and figure of merit into the param_history.

Thanks! In the past, I reran many notebooks where I reverted optax changes to see if that was the cause and it never changed anything - which is why I skipped that here. Pretty sure some other changes caused that.

Good catch, went with the "final params" way here.

Makes sense, that's good to know! It does seem likely the changes are from somewhere else. This all looks good to me, thanks for doing these conversions

@marcorudolphflex marcorudolphflex merged commit c0c168d into develop Jun 11, 2026
3 checks passed
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