torchbase 0.1.0__tar.gz → 0.1.2__tar.gz
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.
- {torchbase-0.1.0 → torchbase-0.1.2}/PKG-INFO +5 -2
- {torchbase-0.1.0 → torchbase-0.1.2}/README.md +4 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/pyproject.toml +6 -2
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/session.py +11 -16
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/session.py +70 -2
- {torchbase-0.1.0 → torchbase-0.1.2}/LICENSE +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/__init__.py +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/__init__.py +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/data.py +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/logger.py +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/metrics.py +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/metrics_instances.py +0 -0
- {torchbase-0.1.0 → torchbase-0.1.2}/torchbase/utils/networks.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: torchbase
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A minimalist base for ML projects in PyTorch
|
|
5
5
|
License: Apache-2.0
|
|
6
6
|
Author: Sohrab Ferdowsi
|
|
@@ -17,13 +17,16 @@ Requires-Dist: numpy
|
|
|
17
17
|
Requires-Dist: scikit-learn
|
|
18
18
|
Requires-Dist: tensorboard
|
|
19
19
|
Requires-Dist: torch
|
|
20
|
-
Requires-Dist: torchinfo
|
|
21
20
|
Description-Content-Type: text/markdown
|
|
22
21
|
|
|
23
22
|
# `torchbase`
|
|
24
23
|
|
|
25
24
|
A minimalist base for ML projects in PyTorch
|
|
26
25
|
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+

|
|
29
|
+
|
|
27
30
|
## Installation
|
|
28
31
|
|
|
29
32
|
Simply pip-install in the environment where your ML projects lies, paying attention to the requirements:
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
A minimalist base for ML projects in PyTorch
|
|
4
4
|
|
|
5
|
+
|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
5
9
|
## Installation
|
|
6
10
|
|
|
7
11
|
Simply pip-install in the environment where your ML projects lies, paying attention to the requirements:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "torchbase"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.2"
|
|
4
4
|
description = "A minimalist base for ML projects in PyTorch"
|
|
5
5
|
authors = ["Sohrab Ferdowsi <sohrab.ferdowsi@gmail.com>"]
|
|
6
6
|
license = "Apache-2.0"
|
|
@@ -14,7 +14,11 @@ datasets = "*"
|
|
|
14
14
|
tensorboard = "*"
|
|
15
15
|
numpy = "*"
|
|
16
16
|
scikit-learn = "*"
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
[tool.poetry.group.dev.dependencies]
|
|
20
|
+
torchvision = "*"
|
|
21
|
+
coverage = "^7.6.10"
|
|
18
22
|
|
|
19
23
|
[build-system]
|
|
20
24
|
requires = ["poetry-core"]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from torchbase.utils.session import TrainingConfigSessionDict
|
|
2
2
|
from torchbase.utils.session import generate_log_dir_tag, is_custom_scalar_logging_layout_valid
|
|
3
|
+
from torchbase.utils.session import RandomnessGeneratorStates
|
|
3
4
|
from torchbase.utils.data import ValidationDatasetsDict
|
|
4
5
|
from torchbase.utils.metrics import BaseMetricsClass
|
|
5
6
|
from torchbase.utils.networks import load_network_from_state_dict_to_device
|
|
@@ -23,7 +24,7 @@ import shutil
|
|
|
23
24
|
|
|
24
25
|
SAVED_NETWORK_NAME = "network.pth"
|
|
25
26
|
SAVED_OPTIMIZER_NAME = "optimizer.pth"
|
|
26
|
-
SAVED_RNG_NAME = "rng_states.
|
|
27
|
+
SAVED_RNG_NAME = "rng_states.json"
|
|
27
28
|
ITERATION_STEPS_TO_SAVE_STATES = 10
|
|
28
29
|
DO_LOG_HPARAMS = True
|
|
29
30
|
|
|
@@ -126,6 +127,11 @@ class TrainingBaseSession(ABC):
|
|
|
126
127
|
return os.path.join(runs_parent_dir, run_dir_tag)
|
|
127
128
|
|
|
128
129
|
else:
|
|
130
|
+
if tag_postfix is not None:
|
|
131
|
+
raise ValueError(
|
|
132
|
+
"It is asked to recover an existing run (rather than creating a new one), yet a tag to post-fix a"
|
|
133
|
+
" (presumably new) run name is assigned.")
|
|
134
|
+
|
|
129
135
|
if not isinstance(source_run_dir_tag, str):
|
|
130
136
|
raise TypeError("By choosing `create_run_dir_afresh = False`, you requested to take over from an "
|
|
131
137
|
"existing `run_dir`. However, the passed `source_run_dir_tag` is not a string.")
|
|
@@ -183,21 +189,14 @@ class TrainingBaseSession(ABC):
|
|
|
183
189
|
states_dir = os.path.join(run_dir, "states")
|
|
184
190
|
|
|
185
191
|
if create_run_dir_afresh:
|
|
192
|
+
rng = RandomnessGeneratorStates()
|
|
186
193
|
os.makedirs(states_dir, exist_ok=False)
|
|
187
|
-
|
|
188
|
-
"cuda_rng_state": torch.cuda.get_rng_state() if torch.cuda.is_available() else None,
|
|
189
|
-
"numpy_rng_state": np.random.get_state(),
|
|
190
|
-
"random_rng_state": random.getstate()}, os.path.join(states_dir, SAVED_RNG_NAME))
|
|
194
|
+
rng.save(os.path.join(states_dir, SAVED_RNG_NAME))
|
|
191
195
|
|
|
192
196
|
else:
|
|
193
197
|
TrainingBaseSession.check_source_states_dir_is_valid(states_dir)
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
torch.set_rng_state(rng_states["torch_rng_state"])
|
|
197
|
-
if rng_states["cuda_rng_state"] is not None:
|
|
198
|
-
torch.cuda.set_rng_state(rng_states["cuda_rng_state"])
|
|
199
|
-
np.random.set_state(rng_states["numpy_rng_state"])
|
|
200
|
-
random.setstate(rng_states["random_rng_state"])
|
|
198
|
+
rng = RandomnessGeneratorStates.load(os.path.join(states_dir, SAVED_RNG_NAME))
|
|
199
|
+
rng.apply()
|
|
201
200
|
|
|
202
201
|
def save_config_to_run_dir(self, create_run_dir_afresh: bool) -> None:
|
|
203
202
|
config = {
|
|
@@ -229,10 +228,6 @@ class TrainingBaseSession(ABC):
|
|
|
229
228
|
if not datasets_valid_dict.is_valid():
|
|
230
229
|
raise ValueError("Failed to create a valid `datasets_valid_dict`, an instance of `ValidationDatasetsDict`.")
|
|
231
230
|
|
|
232
|
-
dataset_train.set_format("torch")
|
|
233
|
-
for dataset in datasets_valid_dict.datasets:
|
|
234
|
-
dataset.set_format("torch")
|
|
235
|
-
|
|
236
231
|
return dataset_train, datasets_valid_dict
|
|
237
232
|
|
|
238
233
|
def init_dataloaders(self) -> Tuple[DataLoader, Dict[str, DataLoader]]:
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
from dataclasses import dataclass, fields, asdict
|
|
2
|
-
|
|
1
|
+
from dataclasses import dataclass, fields, field, asdict
|
|
3
2
|
from typing import Dict, Tuple
|
|
4
3
|
|
|
4
|
+
import torch
|
|
5
|
+
import numpy as np
|
|
6
|
+
import random
|
|
7
|
+
|
|
8
|
+
import json
|
|
5
9
|
import datetime
|
|
6
10
|
import socket
|
|
7
11
|
|
|
@@ -120,3 +124,67 @@ class TrainingConfigSessionDict:
|
|
|
120
124
|
return False
|
|
121
125
|
|
|
122
126
|
return True
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@dataclass
|
|
130
|
+
class RandomnessGeneratorStates:
|
|
131
|
+
torch_state: bytes = field(default_factory=lambda: torch.get_rng_state().numpy().tobytes())
|
|
132
|
+
cuda_state: bytes = field(
|
|
133
|
+
default_factory=lambda: torch.cuda.get_rng_state().numpy().tobytes() if torch.cuda.is_available() else b"")
|
|
134
|
+
numpy_state: tuple = field(default_factory=lambda: np.random.get_state())
|
|
135
|
+
random_state: tuple = field(default_factory=lambda: random.getstate())
|
|
136
|
+
|
|
137
|
+
def save(self, filename: str):
|
|
138
|
+
data = {
|
|
139
|
+
"torch_state": self.torch_state.hex(),
|
|
140
|
+
"cuda_state": self.cuda_state.hex() if self.cuda_state else "",
|
|
141
|
+
"numpy_state": (
|
|
142
|
+
self.numpy_state[0],
|
|
143
|
+
self.numpy_state[1].tolist(),
|
|
144
|
+
*self.numpy_state[2:]
|
|
145
|
+
),
|
|
146
|
+
"random_state": (self.random_state[0], list(self.random_state[1]), self.random_state[2])
|
|
147
|
+
}
|
|
148
|
+
with open(filename, "w") as f:
|
|
149
|
+
json.dump(data, f, indent=2)
|
|
150
|
+
|
|
151
|
+
@classmethod
|
|
152
|
+
def load(cls, filename: str):
|
|
153
|
+
with open(filename, "r") as f:
|
|
154
|
+
data = json.load(f)
|
|
155
|
+
|
|
156
|
+
rng_state = cls()
|
|
157
|
+
rng_state.torch_state = bytes.fromhex(data["torch_state"])
|
|
158
|
+
if data["cuda_state"]:
|
|
159
|
+
rng_state.cuda_state = bytes.fromhex(data["cuda_state"])
|
|
160
|
+
|
|
161
|
+
numpy_state = (
|
|
162
|
+
data["numpy_state"][0],
|
|
163
|
+
np.array(data["numpy_state"][1], dtype=np.uint32),
|
|
164
|
+
*data["numpy_state"][2:]
|
|
165
|
+
)
|
|
166
|
+
rng_state.numpy_state = numpy_state
|
|
167
|
+
|
|
168
|
+
rng_state.random_state = (
|
|
169
|
+
data["random_state"][0],
|
|
170
|
+
tuple(data["random_state"][1]),
|
|
171
|
+
data["random_state"][2]
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
return rng_state
|
|
175
|
+
|
|
176
|
+
def apply(self):
|
|
177
|
+
torch.set_rng_state(torch.tensor(np.frombuffer(self.torch_state, dtype=np.uint8)))
|
|
178
|
+
if self.cuda_state and torch.cuda.is_available():
|
|
179
|
+
torch.cuda.set_rng_state(torch.tensor(np.frombuffer(self.cuda_state, dtype=np.uint8)))
|
|
180
|
+
|
|
181
|
+
numpy_state = (
|
|
182
|
+
str(self.numpy_state[0]),
|
|
183
|
+
np.array(self.numpy_state[1], dtype=np.uint32),
|
|
184
|
+
int(self.numpy_state[2]),
|
|
185
|
+
int(self.numpy_state[3]),
|
|
186
|
+
float(self.numpy_state[4])
|
|
187
|
+
)
|
|
188
|
+
np.random.set_state(numpy_state)
|
|
189
|
+
|
|
190
|
+
random.setstate(self.random_state)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|