-
Notifications
You must be signed in to change notification settings - Fork 66
fix[autograd]: sample fields along slab height and polygon edges #2418
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
f37734b
to
00b99de
Compare
d73a485
to
d188b31
Compare
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.
Thanks @yaugenst-flex. This is pretty complex, I think it will require a lot of testing for both performance and gradient accuracy. Probably in all of the notebooks. I'd say maybe best left to 2.9 to be safe. how much testing in notebooks have you done already? either way will be great to have more accurate gradients for PolySlab!
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.
This is really exciting and will be a huge improvement. Is it possible to run some of the numerical testing and possibly modify the polyslab numerical test file as well to make sure we can test going forward the surface integration (and keep testing cases where it is adding the most improvement).
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.
updates look good to me!
332d8f4
to
c0ab734
Compare
Was in the structure.py file and noticed that in make_adjoint_monitors ( tidy3d/tidy3d/components/structure.py Line 258 in 6b0c432
|
c0ab734
to
95c1619
Compare
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.
11 files reviewed, 4 comments
Edit PR Review Bot Settings | Greptile
0ae943a
to
f03db4f
Compare
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.
293 files reviewed, 1 comment
Edit PR Review Bot Settings | Greptile
@groberts-flex @tylerflex this is ready for final review. Apologize in advance, this got pretty huge. I grouped the changes into a few distinct commits but there is still one monster diff 😅 I ran all relevant notebooks and results are the same as or better than before. In particular the metalens is doing quite a bit better now. |
haven't made it too far on this, but will continue on it tomorrow! really cool how much performance improvement there is. I'm currently running some of the numerical test benchmarks I have for polyslab with it and will especially check when we have thicker polyslabs that are high index and will try some cases with further spaced vertices. |
17067da
to
85414dd
Compare
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/autograd/derivative_utils.py
tidy3d/components/geometry/polyslab.py
tidy3d/components/geometry/primitives.py
|
dc24380
to
674dea9
Compare
e7670d6
to
9ecad6b
Compare
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.
Overall, looks good to me. Left one extra very minor question.
I've also been able to test this numerically and seen either same or improved gradients for those tests. And I was able to test this week that it worked and solved out of the box a sampling issue in polyslab gradient computation for a user.
Everything seems well tested and the performance is great!
2809d02
to
9da31d9
Compare
- Create tidy3d/components/autograd/constants.py - Move MAX_NUM_TRACED_STRUCTURES and MAX_NUM_ADJOINT_PER_FWD from autograd.py - Move PTS_PER_WVL_MAT_CYLINDER_DISCRETIZE from primitives.py
- Add functools.lru_cache decorator to _shapely_is_older_than() - Remove global _SHAPELY_VERSION variable - Cache up to 8 version comparison results - Reduces repeated parsing of version strings in hot paths
… edges - Implement adaptive field sampling for PolySlab and Cylinder for shape derivatives - Replace fixed grid with Gauss-Legendre quadrature - Sample fields along all surface boundaries (edges and faces) - Handle edge cases: slabs outside simulation, faces at ±inf, 2D simulations - Add analytical tests for gradient computation - Remove `DerivativeSurfaceMesh` abstraction in favor of direct evaluation - Convert `DerivativeInfo` from Pydantic model to dataclass for efficiency - Implement interpolator sharing at `GeometryGroup` level - Add Gauss-Legendre quadrature caching - Optimize memory allocation with pre-allocated arrays - Ensure dtype consistency throughout gradient pipeline
9da31d9
to
1422bad
Compare
Previously face gradients came from a fixed 30 × 30 grid so the accuracy depended on the size of the polygon. Edge gradients were evaluated only at the center of each face, so long edges and tall slabs produced incorrect adjoint results depending on the simulation.
Changed:
I also added pretty comprehensive analytic tests for the face and edge integration.
The new implementation is also significantly faster, especially for cases where many structures are included in a geometry group:

Memory usage is about the same as before.
The speedup comes mainly from interpolator caching and removing Pydantic and xarray overhead.
DerivativeInfo
is now a regulardataclass
, and theDerivativeSurfaceMesh
abstraction is gone in favor of a newevaluate_gradient_at_points
function inDerivativeInfo
. Most operations in the "hot path" now use numpy instead of xarray interpolation.Quadrature scheme
First, the code determines a hypothetical number of uniform samples to classify an edge as "short" or "long." Short edges are then evaluated with a direct Gauss quadrature that uses about 40% fewer points than the uniform method. Long edges, which would require more than seven sample points, are instead broken into smaller segments. A 7-point Gauss quadrature is then applied to each segment to maintain accuracy without excessive computational cost.
Greptile Summary
Significantly improves polyslab gradient calculations in autograd by implementing adaptive sampling and performance optimizations. Key changes: