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

Refactor cache_execute() as a transform #5318

Merged
merged 28 commits into from
Mar 25, 2024
Merged

Conversation

Mandrenkov
Copy link
Contributor

@Mandrenkov Mandrenkov commented Mar 5, 2024

Context:

Presently, the cache_execute() function is difficult to understand because it assumes too many responsibilities. This PR offers an alternative implementation, _cache_transform(), which leverages the @transform infrastructure to provide the same functionality but in a shorter, cleaner, and more concise way.

If this PR is approved, we should consider deprecating cache_execute() in the next release.

Description of the Change:

  • Added the following functions:
    • _cache_transform() which re-implements cache_execute() using @transform.
    • _apply_cache_transform() which conveniently wraps _cache_transform().
  • Updated the existing cache_execute() tests to spy on _cache_transform() instead.
  • Added a new set of tests to explicitly test the behaviour of _cache_transform().

Benefits:

  • A significant developer productivity boost for those debugging cache_execute().

Possible Drawbacks:

  • Some additional work will be required to deprecate and eventually remove cache_execute().

Related GitHub Issues:

None.

@Mandrenkov Mandrenkov marked this pull request as ready for review March 5, 2024 22:40
@Mandrenkov Mandrenkov requested a review from albi3ro March 5, 2024 22:40
@Mandrenkov
Copy link
Contributor Author

@albi3ro I've tagged you since you are the primary code owner but I think we should get someone else involved too.

@Mandrenkov Mandrenkov requested a review from albi3ro March 8, 2024 15:09
@Mandrenkov
Copy link
Contributor Author

Thanks for the comment, @albi3ro!

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Sorry about forgetting this aspect.

We should also include a warning if we are using finite-shots, a persistent cache, and re-use a result:

if tape.shots and getattr(cache, "_persistent_cache", True):

@Mandrenkov
Copy link
Contributor Author

Thanks for the updated review, @albi3ro! 🥳

@Mandrenkov Mandrenkov requested a review from albi3ro March 8, 2024 22:51
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

OK I think I see what the failures are. cache=None should be a valid kwarg to _cache_transform, in which case we just return (tape,) , null_postprocessing early.

@Mandrenkov
Copy link
Contributor Author

OK I think I see what the failures are. cache=None should be a valid kwarg to _cache_transform, in which case we just return (tape,) , null_postprocessing early.

Ah, sorry @albi3ro, I missed that the tests failed!

In my opinion, we shouldn't support passing None to _cache_transform because it can hide bugs and encourage bad programming practices: if you don't have a cache, you shouldn't be calling _cache_transform in the first place.

@Mandrenkov Mandrenkov requested a review from albi3ro March 11, 2024 15:22
Copy link

codecov bot commented Mar 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (master@485db04). Click here to learn what that means.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #5318   +/-   ##
=========================================
  Coverage          ?   99.66%           
=========================================
  Files             ?      401           
  Lines             ?    36927           
  Branches          ?        0           
=========================================
  Hits              ?    36802           
  Misses            ?      125           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

So the code cov failures are strictly due the lack of unit tests for cache_execute. Given we are more or less marking that function for removal, I'm fine with leaving those lines uncovered. Though this would potentially be another piece of evidence for removing cache_execute immediately.

But the new transform is so much easier to follow than cache_execute 🎉 Thank you so much for handling this improvement :)

@Mandrenkov

This comment was marked as outdated.

@Mandrenkov
Copy link
Contributor Author

Thanks for the many reviews and advice, @albi3ro!

@Mandrenkov Mandrenkov requested a review from a team March 12, 2024 19:31
@Mandrenkov
Copy link
Contributor Author

After offline discussion, it was decided to remove cache_execute() altogether.

@Mandrenkov Mandrenkov requested a review from albi3ro March 13, 2024 16:58
@Alex-Preciado Alex-Preciado removed the request for review from a team March 13, 2024 21:09
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
@trbromley trbromley requested a review from a team March 14, 2024 13:33
@Alex-Preciado Alex-Preciado removed the request for review from a team March 22, 2024 19:21
@PietropaoloFrisoni
Copy link
Contributor

Thanks, @Mandrenkov. This looks fantastic!
I would only suggest merging from master after 8 days to ensure everything is OK (except for the issue with lightning, which is most probably independent of this PR) 👍

@Mandrenkov Mandrenkov enabled auto-merge (squash) March 25, 2024 17:24
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Reapproving after the removal after removal of cache_execute.

Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

Amazing! Thanks again @Mandrenkov

@Mandrenkov Mandrenkov merged commit 4fc93e0 into master Mar 25, 2024
40 checks passed
@Mandrenkov Mandrenkov deleted the sc-39484-cache-transform branch March 25, 2024 18:29
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.

4 participants