-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
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.
Looks good but would like @yaugenst-flex to take a look since he wrote this part.
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]): |
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.
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.
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 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.
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.
not sure if we need to involve the walrus if we can just assign a variable?
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 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 (edited because stupid)self.data.flat[0]
might error when self.data.size == 0
.
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.
You don't need else
after return
?
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.
oh man 😂 yeah
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.
LGTM, I'm just wondering where this happened to cause a bug? I guess zero-sized data will always lead to an exception somewhere, not necessarily autograd-related?
There's a backend test that tests some edge cases. I think one way this could happen is if you have a TimeMonitor with a start time that is after the time at which the simulation reaches early shutoff. Then the simulation data contains an array with size on the |
To recap should I be changing anything here or not? :) |
I think it's fine as-is. Don't think the flatiter will be a bottleneck 😄 |
agree |
No description provided.