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

[Proposal] change FactoredMatric.svd() so it doesn't prevent all instances of FactoredMatrix from being garbage collected #796

Open
1 task done
manulari opened this issue Nov 25, 2024 · 1 comment
Labels
complexity-moderate Moderately complicated issues for people who have intermediate experience with the code

Comments

@manulari
Copy link

Proposal

FactoredMatrix.svd is annotated with @lru_cache. This prevents instances of FactoredMatrix from ever being garbage collected, as explained here: https://rednafi.com/python/lru_cache_on_methods/

This post also gives a solution, which is to remove the annotation on the method definition, and instead do

def __init__(self, A, B):
    ...
    self.svd = lru_cache()(self._svd)
    ...

def _svd(self):
    ...

Additional context

There is more discussion of the issue in this stackoverflow post.
The python docs also have a faq entry

The solution above still creates a cyclical reference, so requires cycle detection by the garbage collector.

Alternative solution

In this specific case it probably makes more sense to use the @cached_property-decorator from functools instead, as it is in the standard library and designed to be used per instance. It avoids a cyclic reference.

@cached_property
def svd(self):
    ...

Or if for backwards compatibility one wants svd to remain a method and not a property, then one could do:

@cached_property
def _svd(self):
    ...
def svd(self):
    return self._svd

Note that svd being a method is a bit inconsistent anyways, as U, S, Vh are properties.
Also, eigenvalues could be a @cached_property as well.

Checklist

  • I have checked that there is no similar issue in the repo (required)
@bryce13950 bryce13950 added the complexity-moderate Moderately complicated issues for people who have intermediate experience with the code label Nov 25, 2024
@bryce13950
Copy link
Collaborator

Thank you for bringing this up. We can probably work this into the next major release of TransformerLens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complexity-moderate Moderately complicated issues for people who have intermediate experience with the code
Projects
None yet
Development

No branches or pull requests

2 participants