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

Fixing DataArray.tracers for zero-sized data #1818

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
5 changes: 4 additions & 1 deletion tidy3d/components/data/data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def __init__(self, data, *args, **kwargs):

@property
def tracers(self) -> Box:
if AUTOGRAD_KEY not in self.attrs and not isbox(self.data.flat[0]): # no tracers
if self.data.size == 0:
return None
elif AUTOGRAD_KEY not in self.attrs and not isbox(self.data.flat[0]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

might be slightly faster to assign self.data.flat[0] to a variable to avoid doing this twice. but I dont know how expensive this operation really is.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be very fast because it's just a view into the object I believe? But actually yeah this might be a good case for the walrus operator. Was avoiding it because I thought it was introduced way after 3.8... time flies.

Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure if we need to involve the walrus if we can just assign a variable?

Copy link
Collaborator

@yaugenst-flex yaugenst-flex Jul 11, 2024

Choose a reason for hiding this comment

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

This seems pretty clean to me?

@property
def tracers(self) -> Box:
    if self.data.size == 0:
        return None
    elif not isbox(flat := self.data.flat[0]) and AUTOGRAD_KEY not in self.attrs:  # switched order of evaluation so flat is always assigned
        return None
    elif isbox(flat):
        return anp.array(self.values.tolist())
    else:
        return self.attrs[AUTOGRAD_KEY]

Compared to

@property
def tracers(self) -> Box:
    if self.data.size == 0:
        return None

    flat = self.data.flat[0]
    if AUTOGRAD_KEY not in self.attrs and not isbox(flat):
        return None
    elif isbox(flat):
        return anp.array(self.values.tolist())
    else:
        return self.attrs[AUTOGRAD_KEY]

Note that we need to add this level of nesting since self.data.flat[0] might error when self.data.size == 0. (edited because stupid)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You don't need else after return?

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh man 😂 yeah

# no tracers
return None
elif isbox(self.data.flat[0]): # traced values take precedence over traced attrs
return anp.array(self.values.tolist())
Expand Down
Loading