Skip to content

Conversation

@isullivan
Copy link
Contributor

@isullivan isullivan commented Mar 5, 2025

These changes include keeping the input coadds as deferredDatasetHandles until individual tracts are assembled, cleaning up variables in loops, and using indices instead of full arrays in a few places.

Edit: The other PR has merged, so now only commits from this ticket are on this PR

@isullivan isullivan requested a review from parejkoj March 5, 2025 22:27
Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

Several comments; I'm concerned about some of these changes causing either failures or incorrect behavior in ways the current tests won't catch. I don't know for sure that it's wrong, but I'm worried.

I also don't think the dels provide any real benefit.

Comment on lines 284 to 285
del unwarped
del maskedImages
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these can hurt, but they may not reduce memory usage here (the fact that they're C++ objects under the hood also complicates matters).

Maybe put an explicit note above the dels about why they're deleted here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'l certainly add a comment. In my memory profiling, if I leave all of the dels out the total memory usage is ~500MB higher.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please do add that comment.

return geom.Point2D(-10000, -10000)


def generate_data_id(*,
Copy link
Contributor

@parejkoj parejkoj Mar 5, 2025

Choose a reason for hiding this comment

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

ooof, is this really necessary? That's unfortunate. I'd have thought there was an easier way to do this in the middleware, but maybe not if we don't have a real butler?

If you lifted this from elsewhere, is there a place we could put it that both could use instead of having it be copied?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original that I based this on is in lsst.cell_coadds. I certainly don't want to add a dependency on that package here, and my version is modified from that one. This code could be refactored and added to utils or pipe_base, but I would like to do that on a different ticket if possible.

Comment on lines 435 to 437
# Free memory between iterations
del weight
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this does anything, because it gets overwrittten it immediately at the start of the loop, so the old array will go out of scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't need to be in the for loop, and the comment is misleading. The memory savings is from the weight from the last iteration being deleted before new arrays are created after the loop. This saves ~200MB from the peak memory use in my profiling.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I was thinking average memory, but I could see it shaving a bit off the peak.

Comment on lines 419 to 422
good = maskedImage.variance.array > 0
weight = afwImage.ImageF(maskedImage.getBBox())
weight.array[good] = maskedImage.variance.array[good]**(-0.5)
weight = maskedImage.variance.array[good]**(-0.5)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a logic change that is incorrect. good here is a bitmask and I don't know that the assignment below (maskedImage.image.array[good] *= weight) will behave correctly. I also am not sure the tests properly explore the cases here (e.g. bad variance on the edge and in the middle).

I don't know what numpy's behavior is when assigning to a bitmasked-array in this manner, and I'd be a bit surprised if it was fully self-consistent with the old logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maskedImage.variance.array[good] returns a numpy array of only the values of maskedImage.variance.array where good is True. The later assignment maskedImage.image.array[good] *= weight does indeed update just the elements of maskedImage.image.array that correspond to indices where good is True. This is a feature of numpy arrays, which should be stable and reliable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, ok. As I think about it more, since it's a 1d array under the hood, I guess the logic holds. I was just surprised, as I don't think I'd seen this style of usage before.

@isullivan isullivan force-pushed the tickets/DM-49302 branch 2 times, most recently from 098b86e to b0766d3 Compare March 6, 2025 01:45
Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

Please add the comment where you delete a couple images, otherwise this looks good. Hopefully it saves us enough memory and doesn't slow anything down!

@isullivan isullivan merged commit c741bba into main Mar 6, 2025
2 checks passed
@isullivan isullivan deleted the tickets/DM-49302 branch March 6, 2025 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants