nshutils 0.8.0__py3-none-any.whl → 0.11.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.
nshutils/actsave/_saver.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import contextlib
|
2
2
|
import fnmatch
|
3
3
|
import tempfile
|
4
|
-
import uuid
|
5
4
|
import weakref
|
6
5
|
from collections.abc import Callable, Mapping
|
7
6
|
from dataclasses import dataclass
|
@@ -12,6 +11,7 @@ from typing import TYPE_CHECKING, Generic, TypeAlias, cast, overload
|
|
12
11
|
|
13
12
|
import numpy as np
|
14
13
|
from typing_extensions import Never, ParamSpec, TypeVar, override
|
14
|
+
from uuid_extensions import uuid7str
|
15
15
|
|
16
16
|
from ..collections import apply_to_collection
|
17
17
|
|
@@ -236,21 +236,32 @@ class ActSaveProvider:
|
|
236
236
|
_saver: _Saver | None = None
|
237
237
|
_prefixes: list[str] = []
|
238
238
|
|
239
|
-
def
|
239
|
+
def enable(self, save_dir: Path | None = None):
|
240
240
|
"""
|
241
241
|
Initializes the saver with the given configuration and save directory.
|
242
242
|
|
243
243
|
Args:
|
244
244
|
save_dir (Path): The directory where the saved files will be stored.
|
245
245
|
"""
|
246
|
+
if self._saver is not None:
|
247
|
+
log.warning("ActSave is already enabled")
|
248
|
+
return
|
249
|
+
|
250
|
+
if save_dir is None:
|
251
|
+
save_dir = Path(tempfile.gettempdir()) / f"actsave-{uuid7str()}"
|
252
|
+
log.critical(f"No save_dir specified, using {save_dir=}")
|
253
|
+
self._saver = _Saver(save_dir, lambda: self._prefixes)
|
254
|
+
|
255
|
+
def disable(self):
|
256
|
+
"""
|
257
|
+
Disables the actsaver.
|
258
|
+
"""
|
246
259
|
if self._saver is None:
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
lambda: self._prefixes,
|
253
|
-
)
|
260
|
+
log.warning("ActSave is already disabled")
|
261
|
+
return
|
262
|
+
|
263
|
+
del self._saver
|
264
|
+
self._saver = None
|
254
265
|
|
255
266
|
@contextlib.contextmanager
|
256
267
|
def enabled(self, save_dir: Path | None = None):
|
@@ -260,12 +271,16 @@ class ActSaveProvider:
|
|
260
271
|
Args:
|
261
272
|
save_dir (Path): The directory where the saved files will be stored.
|
262
273
|
"""
|
263
|
-
|
264
|
-
|
274
|
+
if self._saver is not None:
|
275
|
+
log.warning("ActSave is already enabled")
|
276
|
+
yield
|
277
|
+
return
|
278
|
+
|
279
|
+
self.enable(save_dir)
|
265
280
|
try:
|
266
281
|
yield
|
267
282
|
finally:
|
268
|
-
self.
|
283
|
+
self.disable()
|
269
284
|
|
270
285
|
@override
|
271
286
|
def __init__(self):
|
@@ -0,0 +1,120 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: nshutils
|
3
|
+
Version: 0.11.0
|
4
|
+
Summary:
|
5
|
+
Author: Nima Shoghi
|
6
|
+
Author-email: nimashoghi@gmail.com
|
7
|
+
Requires-Python: >=3.10,<4.0
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
12
|
+
Provides-Extra: extra
|
13
|
+
Requires-Dist: beartype
|
14
|
+
Requires-Dist: jaxtyping
|
15
|
+
Requires-Dist: numpy
|
16
|
+
Requires-Dist: pysnooper ; extra == "extra"
|
17
|
+
Requires-Dist: typing-extensions
|
18
|
+
Requires-Dist: uuid7
|
19
|
+
Description-Content-Type: text/markdown
|
20
|
+
|
21
|
+
# nshutils
|
22
|
+
|
23
|
+
`nshutils` is a collection of utility functions and classes that I've found useful in my day-to-day work as an ML researcher. This library includes utilities for typechecking, logging, and saving/loading activations from neural networks.
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
To install `nshutils`, simply run:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
pip install nshutils
|
31
|
+
```
|
32
|
+
|
33
|
+
## Features
|
34
|
+
|
35
|
+
### Typechecking
|
36
|
+
|
37
|
+
`nshutils` provides a simple way to typecheck your code using the `jaxtyping` library. Simply call `typecheck_this_module()` at the top of your module (i.e., in the root `__init__.py` file) to enable typechecking for the entire module:
|
38
|
+
|
39
|
+
```python
|
40
|
+
from nshutils.typecheck import typecheck_this_module
|
41
|
+
|
42
|
+
typecheck_this_module()
|
43
|
+
```
|
44
|
+
|
45
|
+
You can also use the `tassert` function to assert that a value is of a certain type:
|
46
|
+
|
47
|
+
```python
|
48
|
+
from nshutils.typecheck import tassert, Float32
|
49
|
+
|
50
|
+
def my_function(x: Float32):
|
51
|
+
tassert(Float32, x)
|
52
|
+
...
|
53
|
+
```
|
54
|
+
|
55
|
+
### Logging
|
56
|
+
|
57
|
+
`nshutils` provides a simple way to configure logging for your project. Simply call one of the logging setup functions:
|
58
|
+
|
59
|
+
```python
|
60
|
+
from nshutils.logging import init_python_logging
|
61
|
+
|
62
|
+
init_python_logging()
|
63
|
+
```
|
64
|
+
|
65
|
+
This will configure logging to use pretty formatting for PyTorch tensors and numpy arrays (inspired by and/or utilizing [`lovely-numpy`](https://github.com/xl0/lovely-numpy) and [`lovely-tensors`](https://github.com/xl0/lovely-tensors)), and will also enable rich logging if the `rich` library is installed.
|
66
|
+
|
67
|
+
### Activation Saving/Loading
|
68
|
+
|
69
|
+
`nshutils` provides a simple way to save and load activations from neural networks. To save activations, use the `ActSave` object:
|
70
|
+
|
71
|
+
```python
|
72
|
+
from nshutils import ActSave
|
73
|
+
|
74
|
+
def my_model_forward(x):
|
75
|
+
...
|
76
|
+
# Save activations to "{save_dir}/encoder.activations/{idx}.npy"
|
77
|
+
ActSave({"encoder.activations": x})
|
78
|
+
|
79
|
+
# Equivalent to the above
|
80
|
+
with ActSave.context("encoder"):
|
81
|
+
ActSave(activations=x)
|
82
|
+
...
|
83
|
+
|
84
|
+
ActSave.enable(save_dir="path/to/activations")
|
85
|
+
x = torch.randn(...)
|
86
|
+
my_model_forward(x)
|
87
|
+
# Activations are saved to disk under the "path/to/activations" directory
|
88
|
+
```
|
89
|
+
|
90
|
+
This will save the `x` tensor to disk under the `encoder` prefix.
|
91
|
+
|
92
|
+
To load activations, use the `ActLoad` class:
|
93
|
+
|
94
|
+
```python
|
95
|
+
from nshutils import ActLoad
|
96
|
+
|
97
|
+
act_load = ActLoad.from_latest_version("path/to/activations")
|
98
|
+
encoder_acts = act_load["encoder"]
|
99
|
+
|
100
|
+
for act in encoder_acts:
|
101
|
+
print(act.shape)
|
102
|
+
```
|
103
|
+
|
104
|
+
This will load all of the activations saved under the `encoder` prefix.
|
105
|
+
|
106
|
+
### Other Utilities
|
107
|
+
|
108
|
+
`nshutils` also provides a few other utility functions/classes:
|
109
|
+
|
110
|
+
- `snoop`: A simple way to debug your code using the `pysnooper` library, based on the [`torchsnooper`](https://github.com/zasdfgbnm/TorchSnooper) library.
|
111
|
+
- `apply_to_collection`: Recursively apply a function to all elements of a collection that match a certain type, taken from the [`pytorch-lightning`](https://github.com/Lightning-AI/pytorch-lightning) library.
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
Contributions to `nshutils` are welcome! Please open an issue or submit a pull request on the [GitHub repository](https://github.com/nimashoghi/nshutils).
|
116
|
+
|
117
|
+
## License
|
118
|
+
|
119
|
+
`nshutils` is released under the MIT License. See the `LICENSE` file for more details.
|
120
|
+
|
@@ -1,11 +1,11 @@
|
|
1
1
|
nshutils/__init__.py,sha256=ZRdp3KcreswdrusXOGc8aEI07qfBCYQGJYC7NqZ5WPI,324
|
2
2
|
nshutils/actsave/__init__.py,sha256=6gJ49011Ad3kS8BejeZRPj7ZyVyIcmX-VKLEmYZyGM8,167
|
3
3
|
nshutils/actsave/_loader.py,sha256=fAhD32DrJa4onkYfcwc21YIeGEYzOSXCK_HVo9SZLgQ,4604
|
4
|
-
nshutils/actsave/_saver.py,sha256
|
4
|
+
nshutils/actsave/_saver.py,sha256=-uKMmMKjEErCa8pfLhAW4077GFcUjxr0Qq0PFAnbEOw,10254
|
5
5
|
nshutils/collections.py,sha256=EE_qLd-LrsX5lsyk9GSKh03Q8bhn9CHB3jiEeNj4uF4,5197
|
6
6
|
nshutils/logging.py,sha256=tL-6XvdvJEEr7bje9DSmUBpm6pnJS9XG_1fuw3U1eME,2573
|
7
7
|
nshutils/snoop.py,sha256=2RNlOcBFnCUyOsfGlbBb8FwSGq03hxfypMfqMA0No-A,7356
|
8
8
|
nshutils/typecheck.py,sha256=_KtfinRy9A0Dgq78kN5MGGrefvb6jn2tGY6svdLEBAs,4915
|
9
|
-
nshutils-0.
|
10
|
-
nshutils-0.
|
11
|
-
nshutils-0.
|
9
|
+
nshutils-0.11.0.dist-info/METADATA,sha256=KIoZDV9c0bjTKbV7F-YOxMthSo_a9J5KOStw92OTa7s,3741
|
10
|
+
nshutils-0.11.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
11
|
+
nshutils-0.11.0.dist-info/RECORD,,
|
@@ -1,19 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: nshutils
|
3
|
-
Version: 0.8.0
|
4
|
-
Summary:
|
5
|
-
Author: Nima Shoghi
|
6
|
-
Author-email: nimashoghi@gmail.com
|
7
|
-
Requires-Python: >=3.10,<4.0
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
9
|
-
Classifier: Programming Language :: Python :: 3.10
|
10
|
-
Classifier: Programming Language :: Python :: 3.11
|
11
|
-
Classifier: Programming Language :: Python :: 3.12
|
12
|
-
Requires-Dist: beartype
|
13
|
-
Requires-Dist: jaxtyping
|
14
|
-
Requires-Dist: numpy
|
15
|
-
Requires-Dist: pysnooper
|
16
|
-
Requires-Dist: typing-extensions
|
17
|
-
Description-Content-Type: text/markdown
|
18
|
-
|
19
|
-
|
File without changes
|