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

Make Outlines' cache reusable across startup by making the cache's key as string #1129

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion outlines/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def wrapper(*args, **kwargs):

def __cache_key__(*args, **kwargs):
"""Make key for cache given function arguments."""
return args_to_key(base, args, kwargs, typed, ignore)
return str(args_to_key(base, args, kwargs, typed, ignore))

wrapper.__cache_key__ = __cache_key__ # type: ignore
wrapper.__memory__ = memory # type: ignore
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
import os
import tempfile
import unittest
Expand Down Expand Up @@ -68,6 +69,33 @@ def f(x):
assert len(store) == store_size + 1


def test_get_cache_from_class_method(test_cache):
store = list()

class DummyObject:
@classmethod
@test_cache
def dummy_function(cls, a):
store.append(a)
return a

@dataclass
class DummyArg:
a: int

dummy_object = DummyObject()

a_1 = DummyArg(1)

dummy_object.dummy_function(a_1)
assert len(store) == 1
store_size = len(store)

a_2 = DummyArg(1)
dummy_object.dummy_function(a_2)
assert len(store) == store_size


def test_disable_cache(test_cache):
"""Make sure that we can disable the cache."""
import outlines
Expand Down