adjoint: add exception for NaN data in adjoint pipeline #2772
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds NaN check for vjp data before adjoint simulations get created
Greptile Summary
This PR adds robust NaN detection and error handling to the adjoint optimization pipeline in Tidy3D's autograd system. The core change introduces a validation check in the
setup_adj
function withintidy3d/web/api/autograd/autograd.py
that scans Vector-Jacobian Product (VJP) data for NaN values before adjoint simulations are created.The implementation strategically places the NaN check after filtering out zero gradient values but before creating adjoint simulations. When NaN values are detected, the system raises an
AdjointError
with a clear, informative message that helps users identify whether the issue stems from simulation data corruption or problems in their objective function computations.This change integrates seamlessly with Tidy3D's existing adjoint optimization workflow, which is used extensively for inverse design applications. The adjoint method computes gradients by running backward simulations, and NaN values in the VJP data can cause these backward simulations to fail or produce invalid gradients silently. By catching these issues early, the system prevents downstream failures and provides actionable feedback to users.
The PR includes comprehensive test coverage through a new parameterized test
test_vjp_nan
that deliberately introduces NaN values into objective functions and verifies proper error handling across different structure and monitor combinations. The change is also properly documented in the changelog, following the project's documentation standards.Important Files Changed
Files Changed
tidy3d/web/api/autograd/autograd.py
tests/test_components/test_autograd.py
CHANGELOG.md
Confidence score: 5/5
Sequence Diagram
Context used:
Rule - Do not use markdown formatting in exception or warning messages; use single quotes to highlight variable names and code. (link)
Rule - Use changelog categories correctly: "Fixed" for bug fixes, "Changed" for modifications to existing functionality, and "Added" for new features. (link)
Rule - In changelogs, enclose code identifiers (class, function names) in backticks and use specific names rather than generic descriptions. (link)
Rule - Prefer pytest.mark.parametrize over manual for loops to define and run distinct test cases, reducing code duplication. (link)
Rule - Assert the direct outcome of an operation rather than a side effect (like a log message) when possible. (link)