nshtrainer 0.3.0__py3-none-any.whl → 0.4.1__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.
- nshtrainer/actsave/__init__.py +2 -6
- nshtrainer/actsave/_callback.py +1 -2
- nshtrainer/runner.py +66 -0
- {nshtrainer-0.3.0.dist-info → nshtrainer-0.4.1.dist-info}/METADATA +3 -3
- {nshtrainer-0.3.0.dist-info → nshtrainer-0.4.1.dist-info}/RECORD +6 -8
- nshtrainer/actsave/_loader.py +0 -144
- nshtrainer/actsave/_saver.py +0 -337
- {nshtrainer-0.3.0.dist-info → nshtrainer-0.4.1.dist-info}/WHEEL +0 -0
nshtrainer/actsave/__init__.py
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
+
from nshutils.actsave import * # type: ignore # noqa: F403
|
|
2
|
+
|
|
1
3
|
from ._callback import ActSaveCallback as ActSaveCallback
|
|
2
|
-
from ._loader import ActivationLoader as ActivationLoader
|
|
3
|
-
from ._loader import ActLoad as ActLoad
|
|
4
|
-
from ._saver import Activation as Activation
|
|
5
|
-
from ._saver import ActivationSaver as ActivationSaver
|
|
6
|
-
from ._saver import ActSave as ActSave
|
|
7
|
-
from ._saver import Transform as Transform
|
nshtrainer/actsave/_callback.py
CHANGED
|
@@ -3,10 +3,9 @@ from typing import TYPE_CHECKING, Literal, cast
|
|
|
3
3
|
|
|
4
4
|
from lightning.pytorch import LightningModule, Trainer
|
|
5
5
|
from lightning.pytorch.callbacks.callback import Callback
|
|
6
|
+
from nshutils.actsave import ActSave
|
|
6
7
|
from typing_extensions import TypeAlias, override
|
|
7
8
|
|
|
8
|
-
from ._saver import ActSave
|
|
9
|
-
|
|
10
9
|
if TYPE_CHECKING:
|
|
11
10
|
from ..model.config import BaseConfig
|
|
12
11
|
|
nshtrainer/runner.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
import functools
|
|
3
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
1
4
|
from typing import Generic
|
|
2
5
|
|
|
3
6
|
from nshrunner import RunInfo
|
|
4
7
|
from nshrunner import Runner as _Runner
|
|
8
|
+
from nshrunner._runner import SnapshotArgType
|
|
5
9
|
from typing_extensions import TypeVar, TypeVarTuple, Unpack, override
|
|
6
10
|
|
|
7
11
|
from .model.config import BaseConfig
|
|
@@ -29,3 +33,65 @@ class Runner(
|
|
|
29
33
|
"id": config.id,
|
|
30
34
|
"base_dir": config.directory.project_root,
|
|
31
35
|
}
|
|
36
|
+
|
|
37
|
+
def _fast_dev_run_transform(
|
|
38
|
+
self,
|
|
39
|
+
config: TConfig,
|
|
40
|
+
*args: Unpack[TArguments],
|
|
41
|
+
n_batches: int,
|
|
42
|
+
):
|
|
43
|
+
config = copy.deepcopy(config)
|
|
44
|
+
config.trainer.fast_dev_run = n_batches
|
|
45
|
+
return (config, *args)
|
|
46
|
+
|
|
47
|
+
def fast_dev_run(
|
|
48
|
+
self,
|
|
49
|
+
runs: Sequence[tuple[TConfig, Unpack[TArguments]]],
|
|
50
|
+
n_batches: int = 1,
|
|
51
|
+
*,
|
|
52
|
+
env: Mapping[str, str] | None = None,
|
|
53
|
+
transforms: list[
|
|
54
|
+
Callable[[TConfig, Unpack[TArguments]], tuple[TConfig, Unpack[TArguments]]]
|
|
55
|
+
]
|
|
56
|
+
| None = None,
|
|
57
|
+
):
|
|
58
|
+
transforms = transforms or []
|
|
59
|
+
transforms.append(
|
|
60
|
+
functools.partial(self._fast_dev_run_transform, n_batches=n_batches)
|
|
61
|
+
)
|
|
62
|
+
return self.local(runs, env=env, transforms=transforms)
|
|
63
|
+
|
|
64
|
+
def fast_dev_run_session(
|
|
65
|
+
self,
|
|
66
|
+
runs: Sequence[tuple[TConfig, Unpack[TArguments]]],
|
|
67
|
+
n_batches: int = 1,
|
|
68
|
+
*,
|
|
69
|
+
snapshot: SnapshotArgType,
|
|
70
|
+
setup_commands: Sequence[str] | None = None,
|
|
71
|
+
env: Mapping[str, str] | None = None,
|
|
72
|
+
transforms: list[
|
|
73
|
+
Callable[[TConfig, Unpack[TArguments]], tuple[TConfig, Unpack[TArguments]]]
|
|
74
|
+
]
|
|
75
|
+
| None = None,
|
|
76
|
+
activate_venv: bool = True,
|
|
77
|
+
session_name: str = "nshrunner",
|
|
78
|
+
attach: bool = True,
|
|
79
|
+
print_command: bool = True,
|
|
80
|
+
pause_before_exit: bool = False,
|
|
81
|
+
):
|
|
82
|
+
transforms = transforms or []
|
|
83
|
+
transforms.append(
|
|
84
|
+
functools.partial(self._fast_dev_run_transform, n_batches=n_batches)
|
|
85
|
+
)
|
|
86
|
+
return self.session(
|
|
87
|
+
runs,
|
|
88
|
+
snapshot=snapshot,
|
|
89
|
+
setup_commands=setup_commands,
|
|
90
|
+
env=env,
|
|
91
|
+
transforms=transforms,
|
|
92
|
+
activate_venv=activate_venv,
|
|
93
|
+
session_name=session_name,
|
|
94
|
+
attach=attach,
|
|
95
|
+
print_command=print_command,
|
|
96
|
+
pause_before_exit=pause_before_exit,
|
|
97
|
+
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: nshtrainer
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary:
|
|
5
5
|
Author: Nima Shoghi
|
|
6
6
|
Author-email: nimashoghi@gmail.com
|
|
@@ -13,8 +13,8 @@ Requires-Dist: lightning
|
|
|
13
13
|
Requires-Dist: lovely-numpy (>=0.2.13,<0.3.0)
|
|
14
14
|
Requires-Dist: lovely-tensors (>=0.1.16,<0.2.0)
|
|
15
15
|
Requires-Dist: nshconfig (>=0.2.0,<0.3.0)
|
|
16
|
-
Requires-Dist: nshrunner (>=0.5.
|
|
17
|
-
Requires-Dist: nshutils (>=0.
|
|
16
|
+
Requires-Dist: nshrunner (>=0.5.5,<0.6.0)
|
|
17
|
+
Requires-Dist: nshutils (>=0.3.0,<0.4.0)
|
|
18
18
|
Requires-Dist: numpy
|
|
19
19
|
Requires-Dist: pytorch-lightning
|
|
20
20
|
Requires-Dist: rich
|
|
@@ -4,10 +4,8 @@ nshtrainer/_experimental/flops/__init__.py,sha256=edo9Ez3LlrnxkNRX9W6YBhPkRPKYGL
|
|
|
4
4
|
nshtrainer/_experimental/flops/flop_counter.py,sha256=-sL0Fy6poXa__hyzUMdZScjPULp4coQELQpPU6p6dXU,25736
|
|
5
5
|
nshtrainer/_experimental/flops/module_tracker.py,sha256=bUL-IRTd0aF_DwmXkZjHZAA31p4ZEhyqhc26XWKQUUY,4922
|
|
6
6
|
nshtrainer/_snoop.py,sha256=2rEemPyMP3aIo2QgPzo_-AlT1oXGWYQipId4RQskMls,58
|
|
7
|
-
nshtrainer/actsave/__init__.py,sha256=
|
|
8
|
-
nshtrainer/actsave/_callback.py,sha256=
|
|
9
|
-
nshtrainer/actsave/_loader.py,sha256=fAhD32DrJa4onkYfcwc21YIeGEYzOSXCK_HVo9SZLgQ,4604
|
|
10
|
-
nshtrainer/actsave/_saver.py,sha256=0EHmQDhqVxQWRWWSyt03eP1K9ETiACMQYmsZkDMt6HY,9451
|
|
7
|
+
nshtrainer/actsave/__init__.py,sha256=_ZuwgRtF1-ekouXNvtZCAS1g_IDYGB4NX8BFSGNGBT8,119
|
|
8
|
+
nshtrainer/actsave/_callback.py,sha256=mnHOtuG9vtHEzz9q4vCvDNC6VvjZsgb4MSSuOoUDh3M,2778
|
|
11
9
|
nshtrainer/callbacks/__init__.py,sha256=I6W33ityL9Ko8jjqHh3WH_8miV59SAe9LxInhoqX5XE,1665
|
|
12
10
|
nshtrainer/callbacks/_throughput_monitor_callback.py,sha256=aJo_11rc4lo0IYOd-kHmPDtzdC4ctgXyRudkRJqH4m4,23184
|
|
13
11
|
nshtrainer/callbacks/base.py,sha256=LrcRUV02bZEKXRIRvhHT9qsvw_kwoWiAdQkVMyKc5NU,3542
|
|
@@ -48,7 +46,7 @@ nshtrainer/nn/module_dict.py,sha256=NOY0B6WDTnktyWH4GthsprMQo0bpehC-hCq9SfD8paE,
|
|
|
48
46
|
nshtrainer/nn/module_list.py,sha256=fb2u5Rqdjff8Pekyr9hkCPkBorQ-fldzzFAjsgWAm30,1719
|
|
49
47
|
nshtrainer/nn/nonlinearity.py,sha256=owtU4kh4G98psD0axOJWVfBhm-OtJVgFM-TXSHmbNPU,3625
|
|
50
48
|
nshtrainer/optimizer.py,sha256=kuJEA1pvB3y1FcsfhAoOJujVqEZqFHlmYO8GW6JeA1g,1527
|
|
51
|
-
nshtrainer/runner.py,sha256=
|
|
49
|
+
nshtrainer/runner.py,sha256=zhNiDmv9R-oVmutQLrNUTDz9RLLAlHG6K0uYz77HkRM,3090
|
|
52
50
|
nshtrainer/scripts/check_env.py,sha256=IMl6dSqsLYppI0XuCsVq8lK4bYqXwY9KHJkzsShz4Kg,806
|
|
53
51
|
nshtrainer/scripts/find_packages.py,sha256=FbdlfmAefttFSMfaT0A46a-oHLP_ioaQKihwBfBeWeA,1467
|
|
54
52
|
nshtrainer/trainer/__init__.py,sha256=P2rmr8oBVTHk-HJHYPcUwWqDEArMbPR4_rPpATbWK3E,40
|
|
@@ -60,6 +58,6 @@ nshtrainer/util/seed.py,sha256=HEXgVs-wldByahOysKwq7506OHxdYTEgmP-tDQVAEkQ,287
|
|
|
60
58
|
nshtrainer/util/slurm.py,sha256=rofIU26z3SdL79SF45tNez6juou1cyDLz07oXEZb9Hg,1566
|
|
61
59
|
nshtrainer/util/typed.py,sha256=NGuDkDzFlc1fAoaXjOFZVbmj0mRFjsQi1E_hPa7Bn5U,128
|
|
62
60
|
nshtrainer/util/typing_utils.py,sha256=8ptjSSLZxlmy4FY6lzzkoGoF5fGNClo8-B_c0XHQaNU,385
|
|
63
|
-
nshtrainer-0.
|
|
64
|
-
nshtrainer-0.
|
|
65
|
-
nshtrainer-0.
|
|
61
|
+
nshtrainer-0.4.1.dist-info/METADATA,sha256=HJFw7NgykuNISf63Lwa7mtnJf8Cpws2x4a_LirXSCnw,812
|
|
62
|
+
nshtrainer-0.4.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
63
|
+
nshtrainer-0.4.1.dist-info/RECORD,,
|
nshtrainer/actsave/_loader.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import pprint
|
|
2
|
-
from dataclasses import dataclass, field
|
|
3
|
-
from functools import cached_property
|
|
4
|
-
from logging import getLogger
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
from typing import cast, overload
|
|
7
|
-
|
|
8
|
-
import numpy as np
|
|
9
|
-
from typing_extensions import TypeVar, override
|
|
10
|
-
|
|
11
|
-
log = getLogger(__name__)
|
|
12
|
-
|
|
13
|
-
T = TypeVar("T", infer_variance=True)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@dataclass
|
|
17
|
-
class LoadedActivation:
|
|
18
|
-
base_dir: Path = field(repr=False)
|
|
19
|
-
name: str
|
|
20
|
-
num_activations: int = field(init=False)
|
|
21
|
-
activation_files: list[Path] = field(init=False, repr=False)
|
|
22
|
-
|
|
23
|
-
def __post_init__(self):
|
|
24
|
-
if not self.activation_dir.exists():
|
|
25
|
-
raise ValueError(f"Activation dir {self.activation_dir} does not exist")
|
|
26
|
-
|
|
27
|
-
# The number of activations = the * of .npy files in the activation dir
|
|
28
|
-
self.activation_files = list(self.activation_dir.glob("*.npy"))
|
|
29
|
-
# Sort the activation files by the numerical index in the filename
|
|
30
|
-
self.activation_files.sort(key=lambda p: int(p.stem))
|
|
31
|
-
self.num_activations = len(self.activation_files)
|
|
32
|
-
|
|
33
|
-
@property
|
|
34
|
-
def activation_dir(self) -> Path:
|
|
35
|
-
return self.base_dir / self.name
|
|
36
|
-
|
|
37
|
-
def _load_activation(self, item: int):
|
|
38
|
-
activation_path = self.activation_files[item]
|
|
39
|
-
if not activation_path.exists():
|
|
40
|
-
raise ValueError(f"Activation {activation_path} does not exist")
|
|
41
|
-
return cast(np.ndarray, np.load(activation_path, allow_pickle=True))
|
|
42
|
-
|
|
43
|
-
@overload
|
|
44
|
-
def __getitem__(self, item: int) -> np.ndarray: ...
|
|
45
|
-
|
|
46
|
-
@overload
|
|
47
|
-
def __getitem__(self, item: slice | list[int]) -> list[np.ndarray]: ...
|
|
48
|
-
|
|
49
|
-
def __getitem__(
|
|
50
|
-
self, item: int | slice | list[int]
|
|
51
|
-
) -> np.ndarray | list[np.ndarray]:
|
|
52
|
-
if isinstance(item, int):
|
|
53
|
-
return self._load_activation(item)
|
|
54
|
-
elif isinstance(item, slice):
|
|
55
|
-
return [
|
|
56
|
-
self._load_activation(i)
|
|
57
|
-
for i in range(*item.indices(self.num_activations))
|
|
58
|
-
]
|
|
59
|
-
elif isinstance(item, list):
|
|
60
|
-
return [self._load_activation(i) for i in item]
|
|
61
|
-
else:
|
|
62
|
-
raise TypeError(f"Invalid type {type(item)} for item {item}")
|
|
63
|
-
|
|
64
|
-
def __iter__(self):
|
|
65
|
-
return iter(self[i] for i in range(self.num_activations))
|
|
66
|
-
|
|
67
|
-
def __len__(self):
|
|
68
|
-
return self.num_activations
|
|
69
|
-
|
|
70
|
-
def all_activations(self):
|
|
71
|
-
return [self[i] for i in range(self.num_activations)]
|
|
72
|
-
|
|
73
|
-
@override
|
|
74
|
-
def __repr__(self):
|
|
75
|
-
return f"<LoadedActivation {self.name} ({self.num_activations} activations)>"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
class ActLoad:
|
|
79
|
-
@classmethod
|
|
80
|
-
def all_versions(cls, dir: str | Path):
|
|
81
|
-
dir = Path(dir)
|
|
82
|
-
|
|
83
|
-
# If the dir is not an activation base directory, we return None
|
|
84
|
-
if not (dir / ".activationbase").exists():
|
|
85
|
-
return None
|
|
86
|
-
|
|
87
|
-
# The contents of `dir` should be directories, each of which is a version.
|
|
88
|
-
return [
|
|
89
|
-
(subdir, int(subdir.name)) for subdir in dir.iterdir() if subdir.is_dir()
|
|
90
|
-
]
|
|
91
|
-
|
|
92
|
-
@classmethod
|
|
93
|
-
def is_valid_activation_base(cls, dir: str | Path):
|
|
94
|
-
return cls.all_versions(dir) is not None
|
|
95
|
-
|
|
96
|
-
@classmethod
|
|
97
|
-
def from_latest_version(cls, dir: str | Path):
|
|
98
|
-
# The contents of `dir` should be directories, each of which is a version
|
|
99
|
-
# We need to find the latest version
|
|
100
|
-
if (all_versions := cls.all_versions(dir)) is None:
|
|
101
|
-
raise ValueError(f"{dir} is not an activation base directory")
|
|
102
|
-
|
|
103
|
-
path, _ = max(all_versions, key=lambda p: p[1])
|
|
104
|
-
return cls(path)
|
|
105
|
-
|
|
106
|
-
def __init__(self, dir: Path):
|
|
107
|
-
self._dir = dir
|
|
108
|
-
|
|
109
|
-
def activation(self, name: str):
|
|
110
|
-
return LoadedActivation(self._dir, name)
|
|
111
|
-
|
|
112
|
-
@cached_property
|
|
113
|
-
def activations(self):
|
|
114
|
-
dirs = list(self._dir.iterdir())
|
|
115
|
-
# Sort the dirs by the last modified time
|
|
116
|
-
dirs.sort(key=lambda p: p.stat().st_mtime)
|
|
117
|
-
|
|
118
|
-
return {p.name: LoadedActivation(self._dir, p.name) for p in dirs}
|
|
119
|
-
|
|
120
|
-
def __iter__(self):
|
|
121
|
-
return iter(self.activations.values())
|
|
122
|
-
|
|
123
|
-
def __getitem__(self, item: str):
|
|
124
|
-
return self.activations[item]
|
|
125
|
-
|
|
126
|
-
def __len__(self):
|
|
127
|
-
return len(self.activations)
|
|
128
|
-
|
|
129
|
-
@override
|
|
130
|
-
def __repr__(self):
|
|
131
|
-
acts_str = pprint.pformat(
|
|
132
|
-
{
|
|
133
|
-
name: f"<{activation.num_activations} activations>"
|
|
134
|
-
for name, activation in self.activations.items()
|
|
135
|
-
}
|
|
136
|
-
)
|
|
137
|
-
acts_str = acts_str.replace("'<", "<").replace(">'", ">")
|
|
138
|
-
return f"ActLoad({acts_str})"
|
|
139
|
-
|
|
140
|
-
def get(self, name: str, /, default: T) -> LoadedActivation | T:
|
|
141
|
-
return self.activations.get(name, default)
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
ActivationLoader = ActLoad
|
nshtrainer/actsave/_saver.py
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
import contextlib
|
|
2
|
-
import fnmatch
|
|
3
|
-
import tempfile
|
|
4
|
-
import uuid
|
|
5
|
-
import weakref
|
|
6
|
-
from collections.abc import Callable, Mapping
|
|
7
|
-
from dataclasses import dataclass
|
|
8
|
-
from functools import wraps
|
|
9
|
-
from logging import getLogger
|
|
10
|
-
from pathlib import Path
|
|
11
|
-
from typing import Generic, TypeAlias, cast, overload
|
|
12
|
-
|
|
13
|
-
import numpy as np
|
|
14
|
-
import torch
|
|
15
|
-
from lightning_utilities.core.apply_func import apply_to_collection
|
|
16
|
-
from typing_extensions import ParamSpec, TypeVar, override
|
|
17
|
-
|
|
18
|
-
log = getLogger(__name__)
|
|
19
|
-
|
|
20
|
-
Value: TypeAlias = int | float | complex | bool | str | np.ndarray | torch.Tensor | None
|
|
21
|
-
ValueOrLambda = Value | Callable[..., Value]
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def _to_numpy(activation: Value) -> np.ndarray:
|
|
25
|
-
# Make sure it's not `None`
|
|
26
|
-
if activation is None:
|
|
27
|
-
raise ValueError("Activation should not be `None`")
|
|
28
|
-
|
|
29
|
-
if isinstance(activation, np.ndarray):
|
|
30
|
-
return activation
|
|
31
|
-
if isinstance(activation, torch.Tensor):
|
|
32
|
-
activation = activation.detach()
|
|
33
|
-
if activation.is_floating_point():
|
|
34
|
-
# NOTE: We need to convert to float32 because [b]float16 is not supported by numpy
|
|
35
|
-
activation = activation.float()
|
|
36
|
-
return activation.cpu().numpy()
|
|
37
|
-
if isinstance(activation, (int, float, complex, str, bool)):
|
|
38
|
-
return np.array(activation)
|
|
39
|
-
|
|
40
|
-
return activation
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
T = TypeVar("T", infer_variance=True)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
# A wrapper around weakref.ref that allows for primitive types
|
|
47
|
-
# To get around errors like:
|
|
48
|
-
# TypeError: cannot create weak reference to 'int' object
|
|
49
|
-
class WeakRef(Generic[T]):
|
|
50
|
-
_ref: Callable[[], T] | None
|
|
51
|
-
|
|
52
|
-
def __init__(self, obj: T):
|
|
53
|
-
try:
|
|
54
|
-
self._ref = cast(Callable[[], T], weakref.ref(obj))
|
|
55
|
-
except TypeError as e:
|
|
56
|
-
if "cannot create weak reference" not in str(e):
|
|
57
|
-
raise
|
|
58
|
-
self._ref = lambda: obj
|
|
59
|
-
|
|
60
|
-
def __call__(self) -> T:
|
|
61
|
-
if self._ref is None:
|
|
62
|
-
raise RuntimeError("WeakRef is deleted")
|
|
63
|
-
return self._ref()
|
|
64
|
-
|
|
65
|
-
def delete(self):
|
|
66
|
-
del self._ref
|
|
67
|
-
self._ref = None
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
@dataclass
|
|
71
|
-
class Activation:
|
|
72
|
-
name: str
|
|
73
|
-
ref: WeakRef[ValueOrLambda] | None
|
|
74
|
-
transformed: np.ndarray | None = None
|
|
75
|
-
|
|
76
|
-
def __post_init__(self):
|
|
77
|
-
# Update the `name` to replace `/` with `.`
|
|
78
|
-
self.name = self.name.replace("/", ".")
|
|
79
|
-
|
|
80
|
-
def __call__(self) -> np.ndarray | None:
|
|
81
|
-
# If we have a transformed value, we return it
|
|
82
|
-
if self.transformed is not None:
|
|
83
|
-
return self.transformed
|
|
84
|
-
|
|
85
|
-
if self.ref is None:
|
|
86
|
-
raise RuntimeError("Activation is deleted")
|
|
87
|
-
|
|
88
|
-
# If we have a lambda, we need to call it
|
|
89
|
-
unrwapped_ref = self.ref()
|
|
90
|
-
activation = unrwapped_ref
|
|
91
|
-
if callable(unrwapped_ref):
|
|
92
|
-
activation = unrwapped_ref()
|
|
93
|
-
|
|
94
|
-
# If we have a `None`, we return early
|
|
95
|
-
if activation is None:
|
|
96
|
-
return None
|
|
97
|
-
|
|
98
|
-
activation = apply_to_collection(activation, torch.Tensor, _to_numpy)
|
|
99
|
-
activation = _to_numpy(activation)
|
|
100
|
-
|
|
101
|
-
# Set the transformed value
|
|
102
|
-
self.transformed = activation
|
|
103
|
-
|
|
104
|
-
# Delete the reference
|
|
105
|
-
self.ref.delete()
|
|
106
|
-
del self.ref
|
|
107
|
-
self.ref = None
|
|
108
|
-
|
|
109
|
-
return self.transformed
|
|
110
|
-
|
|
111
|
-
@classmethod
|
|
112
|
-
def from_value_or_lambda(cls, name: str, value_or_lambda: ValueOrLambda):
|
|
113
|
-
return cls(name, WeakRef(value_or_lambda))
|
|
114
|
-
|
|
115
|
-
@classmethod
|
|
116
|
-
def from_dict(cls, d: Mapping[str, ValueOrLambda]):
|
|
117
|
-
return [cls.from_value_or_lambda(k, v) for k, v in d.items()]
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
Transform = Callable[[Activation], Mapping[str, ValueOrLambda]]
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def _ensure_supported():
|
|
124
|
-
try:
|
|
125
|
-
import torch.distributed as dist
|
|
126
|
-
|
|
127
|
-
if dist.is_initialized() and dist.get_world_size() > 1:
|
|
128
|
-
raise RuntimeError("Only single GPU is supported at the moment")
|
|
129
|
-
except ImportError:
|
|
130
|
-
pass
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
P = ParamSpec("P")
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def _ignore_if_scripting(fn: Callable[P, None]) -> Callable[P, None]:
|
|
137
|
-
@wraps(fn)
|
|
138
|
-
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
|
|
139
|
-
if torch.jit.is_scripting():
|
|
140
|
-
return
|
|
141
|
-
|
|
142
|
-
_ensure_supported()
|
|
143
|
-
fn(*args, **kwargs)
|
|
144
|
-
|
|
145
|
-
return wrapper
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
class _Saver:
|
|
149
|
-
def __init__(
|
|
150
|
-
self,
|
|
151
|
-
save_dir: Path,
|
|
152
|
-
prefixes_fn: Callable[[], list[str]],
|
|
153
|
-
*,
|
|
154
|
-
filters: list[str] | None = None,
|
|
155
|
-
):
|
|
156
|
-
# Create a directory under `save_dir` by autoincrementing
|
|
157
|
-
# (i.e., every activation save context, we create a new directory)
|
|
158
|
-
# The id = the number of activation subdirectories
|
|
159
|
-
self._id = sum(1 for subdir in save_dir.glob("*") if subdir.is_dir())
|
|
160
|
-
save_dir.mkdir(parents=True, exist_ok=True)
|
|
161
|
-
|
|
162
|
-
# Add a .activationbase file to the save_dir to indicate that this is an activation base
|
|
163
|
-
(save_dir / ".activationbase").touch(exist_ok=True)
|
|
164
|
-
|
|
165
|
-
self._save_dir = save_dir / f"{self._id:04d}"
|
|
166
|
-
# Make sure `self._save_dir` does not exist and create it
|
|
167
|
-
self._save_dir.mkdir(exist_ok=False)
|
|
168
|
-
|
|
169
|
-
self._prefixes_fn = prefixes_fn
|
|
170
|
-
self._filters = filters
|
|
171
|
-
|
|
172
|
-
def _save_activation(self, activation: Activation):
|
|
173
|
-
# If the activation value is `None`, we skip it.
|
|
174
|
-
if (activation_value := activation()) is None:
|
|
175
|
-
return
|
|
176
|
-
|
|
177
|
-
# Save the activation to self._save_dir / name / {id}.npz, where id is an auto-incrementing integer
|
|
178
|
-
file_name = ".".join(self._prefixes_fn() + [activation.name])
|
|
179
|
-
path = self._save_dir / file_name
|
|
180
|
-
path.mkdir(exist_ok=True, parents=True)
|
|
181
|
-
|
|
182
|
-
# Get the next id and save the activation
|
|
183
|
-
id = len(list(path.glob("*.npy")))
|
|
184
|
-
np.save(path / f"{id:04d}.npy", activation_value)
|
|
185
|
-
|
|
186
|
-
@_ignore_if_scripting
|
|
187
|
-
def save(
|
|
188
|
-
self,
|
|
189
|
-
acts: dict[str, ValueOrLambda] | None = None,
|
|
190
|
-
/,
|
|
191
|
-
**kwargs: ValueOrLambda,
|
|
192
|
-
):
|
|
193
|
-
kwargs.update(acts or {})
|
|
194
|
-
|
|
195
|
-
# Build activations
|
|
196
|
-
activations = Activation.from_dict(kwargs)
|
|
197
|
-
|
|
198
|
-
for activation in activations:
|
|
199
|
-
# Make sure name matches at least one filter if filters are specified
|
|
200
|
-
if self._filters is not None and all(
|
|
201
|
-
not fnmatch.fnmatch(activation.name, f) for f in self._filters
|
|
202
|
-
):
|
|
203
|
-
continue
|
|
204
|
-
|
|
205
|
-
# Save the current activation
|
|
206
|
-
self._save_activation(activation)
|
|
207
|
-
|
|
208
|
-
del activations
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
class ActSaveProvider:
|
|
212
|
-
_saver: _Saver | None = None
|
|
213
|
-
_prefixes: list[str] = []
|
|
214
|
-
|
|
215
|
-
def initialize(self, save_dir: Path | None = None):
|
|
216
|
-
"""
|
|
217
|
-
Initializes the saver with the given configuration and save directory.
|
|
218
|
-
|
|
219
|
-
Args:
|
|
220
|
-
save_dir (Path): The directory where the saved files will be stored.
|
|
221
|
-
"""
|
|
222
|
-
if self._saver is None:
|
|
223
|
-
if save_dir is None:
|
|
224
|
-
save_dir = Path(tempfile.gettempdir()) / f"actsave-{uuid.uuid4()}"
|
|
225
|
-
log.critical(f"No save_dir specified, using {save_dir=}")
|
|
226
|
-
self._saver = _Saver(
|
|
227
|
-
save_dir,
|
|
228
|
-
lambda: self._prefixes,
|
|
229
|
-
)
|
|
230
|
-
|
|
231
|
-
@contextlib.contextmanager
|
|
232
|
-
def enabled(self, save_dir: Path | None = None):
|
|
233
|
-
"""
|
|
234
|
-
Context manager that enables the actsave functionality with the specified configuration.
|
|
235
|
-
|
|
236
|
-
Args:
|
|
237
|
-
save_dir (Path): The directory where the saved files will be stored.
|
|
238
|
-
"""
|
|
239
|
-
prev = self._saver
|
|
240
|
-
self.initialize(save_dir)
|
|
241
|
-
try:
|
|
242
|
-
yield
|
|
243
|
-
finally:
|
|
244
|
-
self._saver = prev
|
|
245
|
-
|
|
246
|
-
@override
|
|
247
|
-
def __init__(self):
|
|
248
|
-
super().__init__()
|
|
249
|
-
|
|
250
|
-
self._saver = None
|
|
251
|
-
self._prefixes = []
|
|
252
|
-
|
|
253
|
-
@contextlib.contextmanager
|
|
254
|
-
def context(self, label: str):
|
|
255
|
-
"""
|
|
256
|
-
A context manager that adds a label to the current context.
|
|
257
|
-
|
|
258
|
-
Args:
|
|
259
|
-
label (str): The label for the context.
|
|
260
|
-
"""
|
|
261
|
-
if torch.jit.is_scripting():
|
|
262
|
-
yield
|
|
263
|
-
return
|
|
264
|
-
|
|
265
|
-
if self._saver is None:
|
|
266
|
-
yield
|
|
267
|
-
return
|
|
268
|
-
|
|
269
|
-
_ensure_supported()
|
|
270
|
-
|
|
271
|
-
log.debug(f"Entering ActSave context {label}")
|
|
272
|
-
self._prefixes.append(label)
|
|
273
|
-
try:
|
|
274
|
-
yield
|
|
275
|
-
finally:
|
|
276
|
-
_ = self._prefixes.pop()
|
|
277
|
-
|
|
278
|
-
prefix = context
|
|
279
|
-
|
|
280
|
-
@overload
|
|
281
|
-
def __call__(
|
|
282
|
-
self,
|
|
283
|
-
acts: dict[str, ValueOrLambda] | None = None,
|
|
284
|
-
/,
|
|
285
|
-
**kwargs: ValueOrLambda,
|
|
286
|
-
):
|
|
287
|
-
"""
|
|
288
|
-
Saves the activations to disk.
|
|
289
|
-
|
|
290
|
-
Args:
|
|
291
|
-
acts (dict[str, ValueOrLambda] | None, optional): A dictionary of acts. Defaults to None.
|
|
292
|
-
**kwargs (ValueOrLambda): Additional keyword arguments.
|
|
293
|
-
|
|
294
|
-
Returns:
|
|
295
|
-
None
|
|
296
|
-
|
|
297
|
-
"""
|
|
298
|
-
...
|
|
299
|
-
|
|
300
|
-
@overload
|
|
301
|
-
def __call__(self, acts: Callable[[], dict[str, ValueOrLambda]], /):
|
|
302
|
-
"""
|
|
303
|
-
Saves the activations to disk.
|
|
304
|
-
|
|
305
|
-
Args:
|
|
306
|
-
acts (Callable[[], dict[str, ValueOrLambda]]): A callable that returns a dictionary of acts.
|
|
307
|
-
**kwargs (ValueOrLambda): Additional keyword arguments.
|
|
308
|
-
|
|
309
|
-
Returns:
|
|
310
|
-
None
|
|
311
|
-
|
|
312
|
-
"""
|
|
313
|
-
...
|
|
314
|
-
|
|
315
|
-
def __call__(
|
|
316
|
-
self,
|
|
317
|
-
acts: (
|
|
318
|
-
dict[str, ValueOrLambda] | Callable[[], dict[str, ValueOrLambda]] | None
|
|
319
|
-
) = None,
|
|
320
|
-
/,
|
|
321
|
-
**kwargs: ValueOrLambda,
|
|
322
|
-
):
|
|
323
|
-
if torch.jit.is_scripting():
|
|
324
|
-
return
|
|
325
|
-
|
|
326
|
-
if self._saver is None:
|
|
327
|
-
return
|
|
328
|
-
|
|
329
|
-
if acts is not None and callable(acts):
|
|
330
|
-
acts = acts()
|
|
331
|
-
self._saver.save(acts, **kwargs)
|
|
332
|
-
|
|
333
|
-
save = __call__
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
ActSave = ActSaveProvider()
|
|
337
|
-
ActivationSaver = ActSave
|
|
File without changes
|