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

fix deepcopy bug in autograd #1751

Merged
merged 1 commit into from
Jun 10, 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
8 changes: 5 additions & 3 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,15 @@ def updated_copy(self, path: str = None, deep: bool = True, **kwargs) -> Tidy3dB
sub_component = sub_component_list[index]
sub_path = "/".join(path_components[2:])

sub_component_list[index] = sub_component.updated_copy(path=sub_path, **kwargs)
sub_component_list[index] = sub_component.updated_copy(
path=sub_path, deep=deep, **kwargs
)
new_component = tuple(sub_component_list)
else:
sub_path = "/".join(path_components[1:])
new_component = sub_component.updated_copy(path=sub_path, **kwargs)
new_component = sub_component.updated_copy(path=sub_path, deep=deep, **kwargs)

return self._updated_copy(**{field_name: new_component})
return self._updated_copy(deep=deep, **{field_name: new_component})

def _updated_copy(self, deep: bool = True, **kwargs) -> Tidy3dBaseModel:
"""Make copy of a component instance with ``**kwargs`` indicating updated field values."""
Expand Down
17 changes: 17 additions & 0 deletions tidy3d/components/data/data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ def __init__(self, data, *args, **kwargs):
# NOTE: this is done because if we pass the traced array directly, it will create a
# numpy array of `ArrayBox`, which is extremely slow

def __deepcopy__(self, memo):
"""Define the behavior of ``deepcopy()`` a ``xr.DataArray``."""

# if we detect that this has tracers, we need to shallow copy
# otherwise it confuses autograd..
if self.has_tracers:
return self.__copy__()

return super().__deepcopy__(memo)

@property
def has_tracers(self) -> bool:
"""Whether the ``DataArray`` has ``autograd`` derivative information."""
traced_data = self.data.dtype == object and isbox(self.data.flat[0])
traced_attrs = AUTOGRAD_KEY in self.attrs
return traced_data or traced_attrs

@classmethod
def __get_validators__(cls):
"""Validators that get run when :class:`.DataArray` objects are added to pydantic models."""
Expand Down
3 changes: 1 addition & 2 deletions tidy3d/components/geometry/polyslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,8 +1414,7 @@ def compute_derivative_vertices(

# compute center positions between each edge
edge_centers_plane = (vertices_next + vertices) / 2.0
edge_centers_axis = np.mean(self.slab_bounds) * np.ones(num_vertices)

edge_centers_axis = self.center_axis * np.ones(num_vertices)
edge_centers_xyz = self.unpop_axis_vect(edge_centers_axis, edge_centers_plane)

assert edge_centers_xyz.shape == (num_vertices, 3), "something bad happened"
Expand Down
Loading