-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global memo cache and integrate (#130)
Add a global cache dict in `nnbench.types`. Users subclass the `Memo` class with the `@cached_memo` decorator applied to the override of `__call__` method. The call should return the object that should be cached. `del` on the subclassed `Memo` also removes the wrapped item from the cache. Add utility methods for cache management as well as tests. --------- Co-authored-by: Nicholas Junge <[email protected]>
- Loading branch information
1 parent
20e9fe8
commit 636d18b
Showing
3 changed files
with
120 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Generator | ||
|
||
import pytest | ||
|
||
from nnbench.types.types import Memo, cached_memo, clear_memo_cache, memo_cache_size | ||
|
||
|
||
@pytest.fixture | ||
def clear_memos() -> Generator[None, None, None]: | ||
try: | ||
clear_memo_cache() | ||
yield | ||
finally: | ||
clear_memo_cache() | ||
|
||
|
||
class MyMemo(Memo[int]): | ||
@cached_memo | ||
def __call__(self): | ||
return 0 | ||
|
||
|
||
def test_memo_caching(clear_memos): | ||
m = MyMemo() | ||
assert memo_cache_size() == 0 | ||
m() | ||
assert memo_cache_size() == 1 | ||
m() |