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

More autograd improvements #1914

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Error when plotting mode plane PML and the simulation has symmetry.
- Validators using `TriangleMesh.intersections_plane` will fall back on bounding box in case the method fails for a non-watertight mesh.
- Bug when running the same `ModeSolver` first locally then remotely, or vice versa, in which case the cached data from the first run is always returned.
- Gradient monitors for `PolySlab` only store fields at the center location along axis, reducing data usage.
- Validate the forward simulation on the client side even when using `local_gradient=False` for server-side gradient processing.

## [2.7.1] - 2024-07-10

Expand Down
8 changes: 8 additions & 0 deletions tidy3d/components/data/sim_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,14 @@ def make_adjoint_sources(self, data_vjp_paths: set[tuple]) -> dict[str, SourceTy
)
sources_adj_all[mnt_data.monitor.name] = sources_adj

if not sources_adj_all:
raise ValueError(
"No adjoint sources created for this simulation. "
"This could indicate a bug in your setup, for example the objective function "
"output depending on a monitor that is not supported. If you encounter this error, "
"please examine your set up or contact customer support if you need more help."
)

return sources_adj_all

@property
Expand Down
10 changes: 8 additions & 2 deletions tidy3d/components/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .autograd.types import AutogradFieldMap
from .autograd.utils import get_static
from .base import Tidy3dBaseModel, skip_if_fields_missing
from .geometry.polyslab import PolySlab
from .geometry.utils import GeometryType, validate_no_transformed_polyslabs
from .grid.grid import Coords
from .medium import AbstractCustomMedium, Medium2D, MediumType
Expand Down Expand Up @@ -203,8 +204,13 @@ def make_adjoint_monitors(
box = self.geometry.bounding_box

# we dont want these fields getting traced by autograd, otherwise it messes stuff up
size = tuple(get_static(x) for x in box.size) # TODO: expand slightly?
center = tuple(get_static(x) for x in box.center)

size = [get_static(x) for x in box.size] # TODO: expand slightly?
center = [get_static(x) for x in box.center]

# polyslab only needs fields at the midpoint along axis
if isinstance(self.geometry, PolySlab):
size[self.geometry.axis] = 0

mnt_fld = FieldMonitor(
size=size,
Expand Down
12 changes: 7 additions & 5 deletions tidy3d/web/api/autograd/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,14 @@ def _run_primitive(

td.log.info("running primitive '_run_primitive()'")

# compute the combined simulation for both local and remote, so we can validate it
sim_combined = setup_fwd(
sim_fields=sim_fields,
sim_original=sim_original,
local_gradient=local_gradient,
)

if local_gradient:
sim_combined = setup_fwd(
sim_fields=sim_fields,
sim_original=sim_original,
local_gradient=local_gradient,
)
sim_data_combined, _ = _run_tidy3d(sim_combined, task_name=task_name, **run_kwargs)

field_map = postprocess_fwd(
Expand Down
Loading