stringjax-tools 0.1.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.
- stringjax_tools/__init__.py +68 -0
- stringjax_tools/__main__.py +25 -0
- stringjax_tools/_utils.py +201 -0
- stringjax_tools/auto_vectorise.py +581 -0
- stringjax_tools/cache.py +86 -0
- stringjax_tools/jit.py +82 -0
- stringjax_tools/py.typed +1 -0
- stringjax_tools/pytrees.py +331 -0
- stringjax_tools/vmap.py +114 -0
- stringjax_tools-0.1.0.dist-info/METADATA +215 -0
- stringjax_tools-0.1.0.dist-info/RECORD +14 -0
- stringjax_tools-0.1.0.dist-info/WHEEL +5 -0
- stringjax_tools-0.1.0.dist-info/licenses/LICENSE +674 -0
- stringjax_tools-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Copyright 2022-2026 Andreas Schachner
|
|
2
|
+
#
|
|
3
|
+
# This file is part of StringJAX Tools.
|
|
4
|
+
#
|
|
5
|
+
# StringJAX Tools is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# StringJAX Tools is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with StringJAX Tools. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
"""Reusable JAX transform helpers for StringJAX packages."""
|
|
19
|
+
|
|
20
|
+
from .auto_vectorise import (
|
|
21
|
+
ArgSpec,
|
|
22
|
+
auto_vmap,
|
|
23
|
+
auto_vmap_default_shapes,
|
|
24
|
+
auto_vmap_defaults,
|
|
25
|
+
clear_auto_vmap_caches,
|
|
26
|
+
get_auto_vmap_default_shapes,
|
|
27
|
+
get_auto_vmap_defaults,
|
|
28
|
+
reset_auto_vmap_default_shapes,
|
|
29
|
+
reset_auto_vmap_defaults,
|
|
30
|
+
set_auto_vmap_default_shapes,
|
|
31
|
+
set_auto_vmap_defaults,
|
|
32
|
+
)
|
|
33
|
+
from .cache import configure_compilation_cache
|
|
34
|
+
from .jit import is_static, jit_with_dynamic_static_args, jit_with_static_args
|
|
35
|
+
from .pytrees import (
|
|
36
|
+
PytreePolicy,
|
|
37
|
+
flatten_func,
|
|
38
|
+
make_pytree_flatteners,
|
|
39
|
+
unflatten_func_class,
|
|
40
|
+
)
|
|
41
|
+
from .vmap import clear_vmap_caches, vmapping_func, vmapping_func_cached
|
|
42
|
+
|
|
43
|
+
__version__ = "0.1.0"
|
|
44
|
+
|
|
45
|
+
__all__ = [
|
|
46
|
+
"ArgSpec",
|
|
47
|
+
"PytreePolicy",
|
|
48
|
+
"auto_vmap",
|
|
49
|
+
"auto_vmap_default_shapes",
|
|
50
|
+
"auto_vmap_defaults",
|
|
51
|
+
"clear_auto_vmap_caches",
|
|
52
|
+
"clear_vmap_caches",
|
|
53
|
+
"configure_compilation_cache",
|
|
54
|
+
"flatten_func",
|
|
55
|
+
"get_auto_vmap_default_shapes",
|
|
56
|
+
"get_auto_vmap_defaults",
|
|
57
|
+
"is_static",
|
|
58
|
+
"jit_with_dynamic_static_args",
|
|
59
|
+
"jit_with_static_args",
|
|
60
|
+
"make_pytree_flatteners",
|
|
61
|
+
"reset_auto_vmap_default_shapes",
|
|
62
|
+
"reset_auto_vmap_defaults",
|
|
63
|
+
"set_auto_vmap_default_shapes",
|
|
64
|
+
"set_auto_vmap_defaults",
|
|
65
|
+
"unflatten_func_class",
|
|
66
|
+
"vmapping_func",
|
|
67
|
+
"vmapping_func_cached",
|
|
68
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Copyright 2022-2026 Andreas Schachner
|
|
2
|
+
#
|
|
3
|
+
# This file is part of StringJAX Tools.
|
|
4
|
+
#
|
|
5
|
+
# StringJAX Tools is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# StringJAX Tools is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with StringJAX Tools. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
"""Command-line smoke test for ``python -m stringjax_tools``."""
|
|
19
|
+
|
|
20
|
+
from .auto_vectorise import _smoke_test
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
if __name__ == "__main__":
|
|
24
|
+
_smoke_test()
|
|
25
|
+
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Copyright 2022-2026 Andreas Schachner
|
|
2
|
+
#
|
|
3
|
+
# This file is part of StringJAX Tools.
|
|
4
|
+
#
|
|
5
|
+
# StringJAX Tools is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# StringJAX Tools is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with StringJAX Tools. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
"""Internal utility helpers shared by StringJAX Tools modules."""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import inspect
|
|
23
|
+
from collections import OrderedDict
|
|
24
|
+
from collections.abc import Mapping, MutableMapping
|
|
25
|
+
from typing import Any, Callable, TypeVar
|
|
26
|
+
|
|
27
|
+
import numpy as np
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
T = TypeVar("T")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def cache_key_value(value: Any, seen: set[int] | None = None) -> Any:
|
|
34
|
+
r"""
|
|
35
|
+
Return a hashable cache-key component for Python configuration values.
|
|
36
|
+
|
|
37
|
+
Hashable values are keyed by value. Containers are recursively converted
|
|
38
|
+
to stable tuple representations. Ordinary objects are keyed by identity
|
|
39
|
+
and shallow ``__dict__`` state so that simple model-attribute changes can
|
|
40
|
+
create fresh wrapper entries.
|
|
41
|
+
"""
|
|
42
|
+
if seen is None:
|
|
43
|
+
seen = set()
|
|
44
|
+
|
|
45
|
+
object_id = id(value)
|
|
46
|
+
if isinstance(value, np.ndarray):
|
|
47
|
+
return (
|
|
48
|
+
"ndarray",
|
|
49
|
+
value.shape,
|
|
50
|
+
value.dtype.str,
|
|
51
|
+
value.tobytes(),
|
|
52
|
+
)
|
|
53
|
+
if isinstance(value, Mapping):
|
|
54
|
+
if object_id in seen:
|
|
55
|
+
return ("cycle", type(value).__module__, type(value).__qualname__, object_id)
|
|
56
|
+
seen.add(object_id)
|
|
57
|
+
try:
|
|
58
|
+
return (
|
|
59
|
+
"mapping",
|
|
60
|
+
tuple(
|
|
61
|
+
sorted(
|
|
62
|
+
(
|
|
63
|
+
(cache_key_value(key, seen), cache_key_value(val, seen))
|
|
64
|
+
for key, val in value.items()
|
|
65
|
+
),
|
|
66
|
+
key=repr,
|
|
67
|
+
)
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
finally:
|
|
71
|
+
seen.remove(object_id)
|
|
72
|
+
if isinstance(value, tuple):
|
|
73
|
+
if object_id in seen:
|
|
74
|
+
return ("cycle", type(value).__module__, type(value).__qualname__, object_id)
|
|
75
|
+
seen.add(object_id)
|
|
76
|
+
try:
|
|
77
|
+
return ("tuple", tuple(cache_key_value(item, seen) for item in value))
|
|
78
|
+
finally:
|
|
79
|
+
seen.remove(object_id)
|
|
80
|
+
if isinstance(value, list):
|
|
81
|
+
if object_id in seen:
|
|
82
|
+
return ("cycle", type(value).__module__, type(value).__qualname__, object_id)
|
|
83
|
+
seen.add(object_id)
|
|
84
|
+
try:
|
|
85
|
+
return ("list", tuple(cache_key_value(item, seen) for item in value))
|
|
86
|
+
finally:
|
|
87
|
+
seen.remove(object_id)
|
|
88
|
+
if isinstance(value, set):
|
|
89
|
+
if object_id in seen:
|
|
90
|
+
return ("cycle", type(value).__module__, type(value).__qualname__, object_id)
|
|
91
|
+
seen.add(object_id)
|
|
92
|
+
try:
|
|
93
|
+
return (
|
|
94
|
+
"set",
|
|
95
|
+
tuple(sorted((cache_key_value(item, seen) for item in value), key=repr)),
|
|
96
|
+
)
|
|
97
|
+
finally:
|
|
98
|
+
seen.remove(object_id)
|
|
99
|
+
if hasattr(value, "__dict__") and not inspect.ismodule(value):
|
|
100
|
+
if object_id in seen:
|
|
101
|
+
return ("cycle", type(value).__module__, type(value).__qualname__, object_id)
|
|
102
|
+
|
|
103
|
+
seen.add(object_id)
|
|
104
|
+
try:
|
|
105
|
+
state_key = cache_key_value(vars(value), seen)
|
|
106
|
+
finally:
|
|
107
|
+
seen.remove(object_id)
|
|
108
|
+
return (
|
|
109
|
+
"object",
|
|
110
|
+
type(value).__module__,
|
|
111
|
+
type(value).__qualname__,
|
|
112
|
+
object_id,
|
|
113
|
+
state_key,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
hash(value)
|
|
118
|
+
except TypeError:
|
|
119
|
+
return ("id", id(value))
|
|
120
|
+
return ("value", value)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def snapshot_static_value(value: Any, seen: dict[int, Any] | None = None) -> Any:
|
|
124
|
+
r"""
|
|
125
|
+
Snapshot standard mutable containers captured by cached JAX wrappers.
|
|
126
|
+
|
|
127
|
+
Wrapper caches are keyed by the value of Python-side static configuration.
|
|
128
|
+
If a cached closure kept a reference to a mutable list or dict, later
|
|
129
|
+
mutation of that object could invalidate the relation between the cache key
|
|
130
|
+
and the closure's behavior. This helper recursively copies built-in
|
|
131
|
+
containers while leaving ordinary objects by reference, which preserves the
|
|
132
|
+
expected bound-method / model-instance behavior.
|
|
133
|
+
"""
|
|
134
|
+
if seen is None:
|
|
135
|
+
seen = {}
|
|
136
|
+
|
|
137
|
+
object_id = id(value)
|
|
138
|
+
if object_id in seen:
|
|
139
|
+
return seen[object_id]
|
|
140
|
+
|
|
141
|
+
if isinstance(value, np.ndarray):
|
|
142
|
+
snapshot_array = value.copy()
|
|
143
|
+
seen[object_id] = snapshot_array
|
|
144
|
+
return snapshot_array
|
|
145
|
+
if isinstance(value, dict):
|
|
146
|
+
snapshot: dict[Any, Any] = {}
|
|
147
|
+
seen[object_id] = snapshot
|
|
148
|
+
snapshot.update(
|
|
149
|
+
(
|
|
150
|
+
snapshot_static_value(key, seen),
|
|
151
|
+
snapshot_static_value(item, seen),
|
|
152
|
+
)
|
|
153
|
+
for key, item in value.items()
|
|
154
|
+
)
|
|
155
|
+
return snapshot
|
|
156
|
+
if isinstance(value, list):
|
|
157
|
+
snapshot_list: list[Any] = []
|
|
158
|
+
seen[object_id] = snapshot_list
|
|
159
|
+
snapshot_list.extend(snapshot_static_value(item, seen) for item in value)
|
|
160
|
+
return snapshot_list
|
|
161
|
+
if isinstance(value, tuple):
|
|
162
|
+
snapshot_tuple = tuple(snapshot_static_value(item, seen) for item in value)
|
|
163
|
+
seen[object_id] = snapshot_tuple
|
|
164
|
+
return snapshot_tuple
|
|
165
|
+
if isinstance(value, set):
|
|
166
|
+
snapshot_set: set[Any] = set()
|
|
167
|
+
seen[object_id] = snapshot_set
|
|
168
|
+
snapshot_set.update(snapshot_static_value(item, seen) for item in value)
|
|
169
|
+
return snapshot_set
|
|
170
|
+
|
|
171
|
+
return value
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def lru_cache_get(cache: MutableMapping[Any, T], key: Any) -> T | None:
|
|
175
|
+
r"""Return a cached value and refresh its LRU position."""
|
|
176
|
+
try:
|
|
177
|
+
value = cache.pop(key)
|
|
178
|
+
except KeyError:
|
|
179
|
+
return None
|
|
180
|
+
cache[key] = value
|
|
181
|
+
return value
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def lru_cache_set(
|
|
185
|
+
cache: OrderedDict[Any, T],
|
|
186
|
+
key: Any,
|
|
187
|
+
value: T,
|
|
188
|
+
*,
|
|
189
|
+
maxsize: int,
|
|
190
|
+
) -> None:
|
|
191
|
+
r"""Insert ``value`` into a bounded local LRU cache."""
|
|
192
|
+
if key in cache:
|
|
193
|
+
cache.pop(key)
|
|
194
|
+
elif len(cache) >= maxsize:
|
|
195
|
+
cache.popitem(last=False)
|
|
196
|
+
cache[key] = value
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def clear_ordered_cache(cache: OrderedDict[Any, Callable[..., Any]]) -> None:
|
|
200
|
+
r"""Clear a local OrderedDict-backed wrapper cache."""
|
|
201
|
+
cache.clear()
|