checkpointer 2.6.1__py3-none-any.whl → 2.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checkpointer/checkpoint.py +13 -12
- checkpointer/object_hash.py +11 -2
- checkpointer/print_checkpoint.py +1 -1
- checkpointer/utils.py +12 -9
- {checkpointer-2.6.1.dist-info → checkpointer-2.7.0.dist-info}/METADATA +3 -2
- {checkpointer-2.6.1.dist-info → checkpointer-2.7.0.dist-info}/RECORD +8 -8
- {checkpointer-2.6.1.dist-info → checkpointer-2.7.0.dist-info}/WHEEL +0 -0
- {checkpointer-2.6.1.dist-info → checkpointer-2.7.0.dist-info}/licenses/LICENSE +0 -0
checkpointer/checkpoint.py
CHANGED
@@ -26,7 +26,7 @@ class CheckpointerOpts(TypedDict, total=False):
|
|
26
26
|
hash_by: Callable | None
|
27
27
|
should_expire: Callable[[datetime], bool] | None
|
28
28
|
capture: bool
|
29
|
-
fn_hash:
|
29
|
+
fn_hash: ObjectHash | None
|
30
30
|
|
31
31
|
class Checkpointer:
|
32
32
|
def __init__(self, **opts: Unpack[CheckpointerOpts]):
|
@@ -61,14 +61,14 @@ class CheckpointFn(Generic[Fn]):
|
|
61
61
|
return self
|
62
62
|
|
63
63
|
def _lazyinit(self):
|
64
|
+
params = self.checkpointer
|
64
65
|
wrapped = unwrap_fn(self.fn)
|
65
66
|
fn_file = Path(wrapped.__code__.co_filename).name
|
66
67
|
fn_name = re.sub(r"[^\w.]", "", wrapped.__qualname__)
|
67
68
|
update_wrapper(cast(Callable, self), wrapped)
|
68
|
-
|
69
|
-
Storage = STORAGE_MAP[store_format] if isinstance(store_format, str) else store_format
|
69
|
+
Storage = STORAGE_MAP[params.format] if isinstance(params.format, str) else params.format
|
70
70
|
deep_hashes = [child._set_ident().fn_hash_raw for child in iterate_checkpoint_fns(self)]
|
71
|
-
self.fn_hash =
|
71
|
+
self.fn_hash = str(params.fn_hash or ObjectHash().write_text(self.fn_hash_raw, *deep_hashes))
|
72
72
|
self.fn_subdir = f"{fn_file}/{fn_name}/{self.fn_hash[:16]}"
|
73
73
|
self.is_async: bool = self.fn.is_async if isinstance(self.fn, CheckpointFn) else inspect.iscoroutinefunction(self.fn)
|
74
74
|
self.storage = Storage(self)
|
@@ -91,20 +91,21 @@ class CheckpointFn(Generic[Fn]):
|
|
91
91
|
return self
|
92
92
|
|
93
93
|
def get_checkpoint_id(self, args: tuple, kw: dict) -> str:
|
94
|
-
|
95
|
-
|
94
|
+
hash_by = self.checkpointer.hash_by
|
95
|
+
hash_params = hash_by(*args, **kw) if hash_by else (args, kw)
|
96
|
+
call_hash = ObjectHash(hash_params, digest_size=16)
|
96
97
|
return f"{self.fn_subdir}/{call_hash}"
|
97
98
|
|
98
99
|
async def _store_on_demand(self, args: tuple, kw: dict, rerun: bool):
|
100
|
+
params = self.checkpointer
|
99
101
|
checkpoint_id = self.get_checkpoint_id(args, kw)
|
100
|
-
checkpoint_path =
|
101
|
-
verbosity = self.checkpointer.verbosity
|
102
|
+
checkpoint_path = params.root_path / checkpoint_id
|
102
103
|
refresh = rerun \
|
103
104
|
or not self.storage.exists(checkpoint_path) \
|
104
|
-
or (
|
105
|
+
or (params.should_expire and params.should_expire(self.storage.checkpoint_date(checkpoint_path)))
|
105
106
|
|
106
107
|
if refresh:
|
107
|
-
print_checkpoint(verbosity >= 1, "MEMORIZING", checkpoint_id, "blue")
|
108
|
+
print_checkpoint(params.verbosity >= 1, "MEMORIZING", checkpoint_id, "blue")
|
108
109
|
data = self.fn(*args, **kw)
|
109
110
|
if inspect.iscoroutine(data):
|
110
111
|
data = await data
|
@@ -113,11 +114,11 @@ class CheckpointFn(Generic[Fn]):
|
|
113
114
|
|
114
115
|
try:
|
115
116
|
data = self.storage.load(checkpoint_path)
|
116
|
-
print_checkpoint(verbosity >= 2, "REMEMBERED", checkpoint_id, "green")
|
117
|
+
print_checkpoint(params.verbosity >= 2, "REMEMBERED", checkpoint_id, "green")
|
117
118
|
return data
|
118
119
|
except (EOFError, FileNotFoundError):
|
119
120
|
pass
|
120
|
-
print_checkpoint(verbosity >= 1, "CORRUPTED", checkpoint_id, "yellow")
|
121
|
+
print_checkpoint(params.verbosity >= 1, "CORRUPTED", checkpoint_id, "yellow")
|
121
122
|
return await self._store_on_demand(args, kw, True)
|
122
123
|
|
123
124
|
def _call(self, args: tuple, kw: dict, rerun=False):
|
checkpointer/object_hash.py
CHANGED
@@ -2,23 +2,32 @@ import ctypes
|
|
2
2
|
import hashlib
|
3
3
|
import io
|
4
4
|
import re
|
5
|
+
import sys
|
5
6
|
from collections.abc import Iterable
|
6
7
|
from contextlib import nullcontext, suppress
|
7
8
|
from decimal import Decimal
|
8
9
|
from itertools import chain
|
9
10
|
from pickle import HIGHEST_PROTOCOL as PROTOCOL
|
10
11
|
from types import BuiltinFunctionType, FunctionType, GeneratorType, MethodType, ModuleType, UnionType
|
11
|
-
from typing import Any,
|
12
|
+
from typing import Any, TypeVar
|
12
13
|
from .utils import ContextVar, get_fn_body
|
13
14
|
|
14
15
|
np, torch = None, None
|
15
16
|
|
16
17
|
with suppress(Exception):
|
17
18
|
import numpy as np
|
18
|
-
|
19
19
|
with suppress(Exception):
|
20
20
|
import torch
|
21
21
|
|
22
|
+
class _Never:
|
23
|
+
def __getattribute__(self, _: str):
|
24
|
+
pass
|
25
|
+
|
26
|
+
if sys.version_info >= (3, 12):
|
27
|
+
from typing import TypeAliasType
|
28
|
+
else:
|
29
|
+
TypeAliasType = _Never
|
30
|
+
|
22
31
|
def encode_type(t: type | FunctionType) -> str:
|
23
32
|
return f"{t.__module__}:{t.__qualname__}"
|
24
33
|
|
checkpointer/print_checkpoint.py
CHANGED
@@ -49,4 +49,4 @@ colored = colored_ if allow_color() else noop
|
|
49
49
|
|
50
50
|
def print_checkpoint(should_log: bool, title: str, text: str, color: Color):
|
51
51
|
if should_log:
|
52
|
-
print(f
|
52
|
+
print(f'{colored(f" {title} ", "grey", color)} {colored(text, color)}')
|
checkpointer/utils.py
CHANGED
@@ -3,9 +3,12 @@ import tokenize
|
|
3
3
|
from contextlib import contextmanager
|
4
4
|
from io import StringIO
|
5
5
|
from types import coroutine
|
6
|
-
from typing import Any, Callable, Coroutine, Generator, Iterable, cast
|
6
|
+
from typing import Any, Callable, Coroutine, Generator, Generic, Iterable, TypeVar, cast
|
7
7
|
|
8
|
-
|
8
|
+
T = TypeVar("T")
|
9
|
+
T_Callable = TypeVar("T_Callable", bound=Callable)
|
10
|
+
|
11
|
+
def distinct(seq: Iterable[T]) -> list[T]:
|
9
12
|
return list(dict.fromkeys(seq))
|
10
13
|
|
11
14
|
def transpose(tuples, default_num_returns=0):
|
@@ -30,22 +33,22 @@ def get_cell_contents(fn: Callable) -> Iterable[tuple[str, Any]]:
|
|
30
33
|
except ValueError:
|
31
34
|
pass
|
32
35
|
|
33
|
-
def unwrap_fn
|
36
|
+
def unwrap_fn(fn: T_Callable, checkpoint_fn=False) -> T_Callable:
|
34
37
|
from .checkpoint import CheckpointFn
|
35
38
|
while True:
|
36
39
|
if (checkpoint_fn and isinstance(fn, CheckpointFn)) or not hasattr(fn, "__wrapped__"):
|
37
|
-
return cast(
|
40
|
+
return cast(T_Callable, fn)
|
38
41
|
fn = getattr(fn, "__wrapped__")
|
39
42
|
|
40
|
-
async def resolved_awaitable
|
43
|
+
async def resolved_awaitable(value: T) -> T:
|
41
44
|
return value
|
42
45
|
|
43
46
|
@coroutine
|
44
|
-
def coroutine_as_generator
|
47
|
+
def coroutine_as_generator(coroutine: Coroutine[None, None, T]) -> Generator[None, None, T]:
|
45
48
|
val = yield from coroutine
|
46
49
|
return val
|
47
50
|
|
48
|
-
def sync_resolve_coroutine
|
51
|
+
def sync_resolve_coroutine(coroutine: Coroutine[None, None, T]) -> T:
|
49
52
|
gen = cast(Generator, coroutine_as_generator(coroutine))
|
50
53
|
try:
|
51
54
|
while True:
|
@@ -81,7 +84,7 @@ class AttrDict(dict):
|
|
81
84
|
d = getattr(d, attr, None)
|
82
85
|
return d
|
83
86
|
|
84
|
-
class ContextVar[T]:
|
87
|
+
class ContextVar(Generic[T]):
|
85
88
|
def __init__(self, value: T):
|
86
89
|
self.value = value
|
87
90
|
|
@@ -93,7 +96,7 @@ class ContextVar[T]:
|
|
93
96
|
finally:
|
94
97
|
self.value = old
|
95
98
|
|
96
|
-
class iterate_and_upcoming[T]:
|
99
|
+
class iterate_and_upcoming(Generic[T]):
|
97
100
|
def __init__(self, it: Iterable[T]) -> None:
|
98
101
|
self.it = iter(it)
|
99
102
|
self.previous: tuple[()] | tuple[T] = ()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: checkpointer
|
3
|
-
Version: 2.
|
3
|
+
Version: 2.7.0
|
4
4
|
Summary: A Python library for memoizing function results with support for multiple storage backends, async runtimes, and automatic cache invalidation
|
5
5
|
Project-URL: Repository, https://github.com/Reddan/checkpointer.git
|
6
6
|
Author: Hampus Hallman
|
@@ -12,9 +12,10 @@ License: Copyright 2018-2025 Hampus Hallman
|
|
12
12
|
|
13
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
14
14
|
License-File: LICENSE
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
15
16
|
Classifier: Programming Language :: Python :: 3.12
|
16
17
|
Classifier: Programming Language :: Python :: 3.13
|
17
|
-
Requires-Python: >=3.
|
18
|
+
Requires-Python: >=3.11
|
18
19
|
Description-Content-Type: text/markdown
|
19
20
|
|
20
21
|
# checkpointer · [](https://github.com/Reddan/checkpointer/blob/master/LICENSE) [](https://pypi.org/project/checkpointer/) [](https://pypi.org/project/checkpointer/)
|
@@ -1,16 +1,16 @@
|
|
1
1
|
checkpointer/__init__.py,sha256=ZJ6frUNgkklUi85b5uXTyTfRzMvZgQOJY-ZOnu7jh78,777
|
2
|
-
checkpointer/checkpoint.py,sha256=
|
2
|
+
checkpointer/checkpoint.py,sha256=NmspwYKHbhaTMElENXR3TREzvhaXYfmuz_rQJsLUtpA,6280
|
3
3
|
checkpointer/fn_ident.py,sha256=SWaksNCTlskMom0ztqjECSRjZYPWXUA1p1ZCb-9tWo0,4297
|
4
|
-
checkpointer/object_hash.py,sha256=
|
5
|
-
checkpointer/print_checkpoint.py,sha256=
|
4
|
+
checkpointer/object_hash.py,sha256=rcHzVYZAeygLyqeKv1NODIDp0M_knLuDZefcBV_7ln4,7371
|
5
|
+
checkpointer/print_checkpoint.py,sha256=aJCeWMRJiIR3KpyPk_UOKTaD906kArGrmLGQ3LqcVgo,1369
|
6
6
|
checkpointer/test_checkpointer.py,sha256=uJ2Pg9Miq1W0l28eNlRhMjuT_R8c-ygYwp3KP3VW8Os,3600
|
7
|
-
checkpointer/utils.py,sha256=
|
7
|
+
checkpointer/utils.py,sha256=E1AV96NTh3tuVmgPrr0JSKZaokw-Jely5Y6-NjlMCp8,3141
|
8
8
|
checkpointer/storages/__init__.py,sha256=Kl4Og5jhYxn6m3tB_kTMsabf4_eWVLmFVAoC-pikNQE,301
|
9
9
|
checkpointer/storages/bcolz_storage.py,sha256=3QkSUSeG5s2kFuVV_LZpzMn1A5E7kqC7jk7w35c0NyQ,2314
|
10
10
|
checkpointer/storages/memory_storage.py,sha256=S5ayOZE_CyaFQJ-vSgObTanldPzG3gh3NksjNAc7vsk,1282
|
11
11
|
checkpointer/storages/pickle_storage.py,sha256=idh9sBMdWuyvS220oa_7bAUpc9Xo9v6Ud9aYKGWasUs,1593
|
12
12
|
checkpointer/storages/storage.py,sha256=_m18Z8TKrdAbi6YYYQmuNOnhna4RB2sJDn1v3liaU3U,721
|
13
|
-
checkpointer-2.
|
14
|
-
checkpointer-2.
|
15
|
-
checkpointer-2.
|
16
|
-
checkpointer-2.
|
13
|
+
checkpointer-2.7.0.dist-info/METADATA,sha256=ANOUJpdq3OXaF_d9jMBP0rWlpIOq0c4Z2uVKjXf_YnE,10606
|
14
|
+
checkpointer-2.7.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
15
|
+
checkpointer-2.7.0.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
16
|
+
checkpointer-2.7.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|