useml 0.0.2__tar.gz → 0.1.0__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.
- useml-0.1.0/PKG-INFO +211 -0
- useml-0.1.0/README.md +194 -0
- {useml-0.0.2 → useml-0.1.0}/pyproject.toml +6 -4
- useml-0.1.0/tests/test_session.py +211 -0
- useml-0.1.0/tests/test_train_level0.py +226 -0
- useml-0.1.0/tests/test_vault.py +242 -0
- useml-0.1.0/useml/__init__.py +213 -0
- useml-0.1.0/useml/errors.py +364 -0
- useml-0.1.0/useml/imports.py +215 -0
- useml-0.1.0/useml/session/__init__.py +4 -0
- useml-0.1.0/useml/session/component.py +59 -0
- useml-0.1.0/useml/session/manager.py +431 -0
- useml-0.1.0/useml/template/__init__.py +6 -0
- useml-0.1.0/useml/template/config.py +100 -0
- useml-0.1.0/useml/template/dataset.py +160 -0
- useml-0.1.0/useml/template/loss.py +28 -0
- useml-0.1.0/useml/template/model.py +28 -0
- useml-0.1.0/useml/template/trainer.py +228 -0
- useml-0.1.0/useml/vault/__init__.py +5 -0
- useml-0.1.0/useml/vault/code_extractor.py +258 -0
- {useml-0.0.2 → useml-0.1.0}/useml/vault/core.py +21 -11
- useml-0.1.0/useml/vault/project.py +161 -0
- useml-0.1.0/useml/vault/snapshot.py +170 -0
- useml-0.1.0/useml/workdir/__init__.py +14 -0
- useml-0.1.0/useml/workdir/_hook.py +79 -0
- useml-0.1.0/useml.egg-info/PKG-INFO +211 -0
- useml-0.1.0/useml.egg-info/SOURCES.txt +29 -0
- {useml-0.0.2 → useml-0.1.0}/useml.egg-info/requires.txt +1 -2
- useml-0.0.2/PKG-INFO +0 -99
- useml-0.0.2/README.md +0 -81
- useml-0.0.2/tests/test_vault.py +0 -135
- useml-0.0.2/useml/__init__.py +0 -6
- useml-0.0.2/useml/vault/project.py +0 -110
- useml-0.0.2/useml/vault/snapshot.py +0 -130
- useml-0.0.2/useml.egg-info/PKG-INFO +0 -99
- useml-0.0.2/useml.egg-info/SOURCES.txt +0 -12
- {useml-0.0.2 → useml-0.1.0}/setup.cfg +0 -0
- {useml-0.0.2 → useml-0.1.0}/useml.egg-info/dependency_links.txt +0 -0
- {useml-0.0.2 → useml-0.1.0}/useml.egg-info/top_level.txt +0 -0
useml-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: useml
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Minimalist ML experiment versioning for PyTorch — checkpointing, source archiving, and model isolation.
|
|
5
|
+
Author-email: Enzo-py <85191552+Enzo-py@users.noreply.github.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/Enzo-py/useml
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/Enzo-py/useml/issues
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: torch>=2.0.0
|
|
11
|
+
Requires-Dist: pyyaml>=6.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
14
|
+
Requires-Dist: build; extra == "dev"
|
|
15
|
+
Requires-Dist: twine; extra == "dev"
|
|
16
|
+
Requires-Dist: black; extra == "dev"
|
|
17
|
+
|
|
18
|
+
# useml
|
|
19
|
+
|
|
20
|
+
**Stop coding plumbing, start training models.**
|
|
21
|
+
|
|
22
|
+
`useml` is a minimalist ML experiment versioning framework for PyTorch. It handles checkpointing, source archiving, and model isolation so you can focus on the architecture.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install useml
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Core concepts
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
vault/
|
|
34
|
+
└── my-experiment/ ← project
|
|
35
|
+
├── snap_20240501_.../ ← snapshot (one per commit)
|
|
36
|
+
│ ├── weights/ ← model .pth files
|
|
37
|
+
│ ├── configs/ ← per-component YAML configs
|
|
38
|
+
│ ├── source/ ← archived source code
|
|
39
|
+
│ ├── manifest.yaml ← component registry
|
|
40
|
+
│ └── metadata.yaml ← message + metrics
|
|
41
|
+
└── snap_20240502_.../
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Basic usage
|
|
45
|
+
|
|
46
|
+
### Session setup
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import useml
|
|
50
|
+
|
|
51
|
+
useml.init("vault") # connect to a vault directory
|
|
52
|
+
useml.new("my-experiment") # create and focus a project
|
|
53
|
+
# or: useml.focus("my-experiment") to resume an existing one
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Track → Commit loop
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import torch.nn as nn
|
|
60
|
+
import useml
|
|
61
|
+
|
|
62
|
+
useml.init("vault")
|
|
63
|
+
useml.new("mnist-run", auto_focus=True)
|
|
64
|
+
|
|
65
|
+
model = nn.Linear(784, 10)
|
|
66
|
+
useml.track("classifier", model)
|
|
67
|
+
|
|
68
|
+
# ... train ...
|
|
69
|
+
|
|
70
|
+
snap = useml.commit("baseline", val_loss=0.42, accuracy=0.91)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Multiple models, per-model configs
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
useml.track("encoder", encoder, config=encoder_config)
|
|
77
|
+
useml.track("decoder", decoder, config=decoder_config)
|
|
78
|
+
useml.commit("joint training", val_loss=0.18)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Config
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from useml import Config
|
|
85
|
+
|
|
86
|
+
config = Config(
|
|
87
|
+
epochs=30,
|
|
88
|
+
lr=1e-3,
|
|
89
|
+
optimizer="adamw", # "adam" | "adamw" | "sgd"
|
|
90
|
+
loss="cross_entropy", # str key, nn.Module class/instance, or callable
|
|
91
|
+
batch_size=128,
|
|
92
|
+
checkpoint_every=5, # commit every N epochs during useml.train()
|
|
93
|
+
device="auto", # "auto" | "cpu" | "cuda" | "mps"
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Dashboard
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
useml.show()
|
|
101
|
+
# --- UseML Dashboard ---
|
|
102
|
+
# Vault: vault/
|
|
103
|
+
# Project: my-experiment (3 snapshots)
|
|
104
|
+
# Tracked: ['classifier']
|
|
105
|
+
# Latest: baseline [snap_20240501_...]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Level-0 API — train in two lines
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import useml
|
|
112
|
+
from useml import Config, Model
|
|
113
|
+
import torch.nn as nn
|
|
114
|
+
|
|
115
|
+
class Net(Model):
|
|
116
|
+
def __init__(self):
|
|
117
|
+
super().__init__()
|
|
118
|
+
self.fc = nn.Linear(784, 10)
|
|
119
|
+
def forward(self, x):
|
|
120
|
+
return self.fc(x.view(x.size(0), -1))
|
|
121
|
+
|
|
122
|
+
useml.init("vault")
|
|
123
|
+
useml.new("mnist-quick", auto_focus=True)
|
|
124
|
+
|
|
125
|
+
history = useml.train(Net, "mnist", config=Config(epochs=10))
|
|
126
|
+
# Built-in datasets: "mnist", "fashion_mnist", "cifar10", "cifar100"
|
|
127
|
+
# HuggingFace: "hf:<dataset_name>"
|
|
128
|
+
# Custom: any torch.utils.data.Dataset
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Loading models
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
# Load latest weights (uses current code)
|
|
135
|
+
model = useml.load("classifier")
|
|
136
|
+
|
|
137
|
+
# Load with full code isolation (archived source + weights)
|
|
138
|
+
useml.mount("\\latest")
|
|
139
|
+
model = useml.load("classifier")
|
|
140
|
+
|
|
141
|
+
# Load from a specific snapshot
|
|
142
|
+
model = useml.load("classifier", _from="\\head~2")
|
|
143
|
+
|
|
144
|
+
# Import archived code directly
|
|
145
|
+
useml.mount("\\latest")
|
|
146
|
+
from useml.workdir.models import Net
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Snapshot tags
|
|
150
|
+
|
|
151
|
+
| Tag | Meaning |
|
|
152
|
+
|---|---|
|
|
153
|
+
| `\latest` | Most recent snapshot |
|
|
154
|
+
| `\head~N` | N snapshots before latest |
|
|
155
|
+
| `\current` / `\workdir` | Unmount (back to live code) |
|
|
156
|
+
| `snap_20240501_...` | Exact snapshot folder name |
|
|
157
|
+
|
|
158
|
+
## Custom loss
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
# String key (built-in)
|
|
162
|
+
Config(loss="cross_entropy") # also: "mse", "bce", "l1"
|
|
163
|
+
|
|
164
|
+
# Class
|
|
165
|
+
Config(loss=nn.HuberLoss)
|
|
166
|
+
|
|
167
|
+
# Instance
|
|
168
|
+
Config(loss=nn.HuberLoss(delta=0.5))
|
|
169
|
+
|
|
170
|
+
# Plain function
|
|
171
|
+
def focal_loss(pred, target): ...
|
|
172
|
+
Config(loss=focal_loss)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The loss source is automatically archived in the snapshot.
|
|
176
|
+
|
|
177
|
+
## Stashing (multi-project workflows)
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
useml.focus("project-a")
|
|
181
|
+
useml.track("model", model_a)
|
|
182
|
+
useml.stash() # park project-a in RAM
|
|
183
|
+
|
|
184
|
+
useml.focus("project-b")
|
|
185
|
+
useml.track("model", model_b)
|
|
186
|
+
useml.commit("run b")
|
|
187
|
+
|
|
188
|
+
useml.focus("project-a") # restore from stash
|
|
189
|
+
useml.commit("run a")
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Error codes
|
|
193
|
+
|
|
194
|
+
All exceptions raised by useml carry a `[UML-NNN]` code. See [ERRORS.md](ERRORS.md) for the full catalog with causes and fixes.
|
|
195
|
+
|
|
196
|
+
## API reference
|
|
197
|
+
|
|
198
|
+
| Function | Description |
|
|
199
|
+
|---|---|
|
|
200
|
+
| `useml.init(vault_path)` | Connect session to a vault directory |
|
|
201
|
+
| `useml.new(name)` | Create and focus a new project |
|
|
202
|
+
| `useml.focus(name)` | Resume an existing project |
|
|
203
|
+
| `useml.track(name, model, config?, optimizer?)` | Register a component |
|
|
204
|
+
| `useml.commit(message, **metrics)` | Save a snapshot |
|
|
205
|
+
| `useml.load(name, _from?)` | Restore a model with weights |
|
|
206
|
+
| `useml.mount(tag)` | Mount a snapshot for workdir imports |
|
|
207
|
+
| `useml.show()` | Print session dashboard |
|
|
208
|
+
| `useml.stash()` | Park current project state in RAM |
|
|
209
|
+
| `useml.projects()` | List all projects in the vault |
|
|
210
|
+
| `useml.train(model_cls, dataset, config?)` | Level-0 training entry point |
|
|
211
|
+
| `useml.debug_imports()` | Inspect workdir importable modules |
|
useml-0.1.0/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# useml
|
|
2
|
+
|
|
3
|
+
**Stop coding plumbing, start training models.**
|
|
4
|
+
|
|
5
|
+
`useml` is a minimalist ML experiment versioning framework for PyTorch. It handles checkpointing, source archiving, and model isolation so you can focus on the architecture.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install useml
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Core concepts
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
vault/
|
|
17
|
+
└── my-experiment/ ← project
|
|
18
|
+
├── snap_20240501_.../ ← snapshot (one per commit)
|
|
19
|
+
│ ├── weights/ ← model .pth files
|
|
20
|
+
│ ├── configs/ ← per-component YAML configs
|
|
21
|
+
│ ├── source/ ← archived source code
|
|
22
|
+
│ ├── manifest.yaml ← component registry
|
|
23
|
+
│ └── metadata.yaml ← message + metrics
|
|
24
|
+
└── snap_20240502_.../
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Basic usage
|
|
28
|
+
|
|
29
|
+
### Session setup
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import useml
|
|
33
|
+
|
|
34
|
+
useml.init("vault") # connect to a vault directory
|
|
35
|
+
useml.new("my-experiment") # create and focus a project
|
|
36
|
+
# or: useml.focus("my-experiment") to resume an existing one
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Track → Commit loop
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import torch.nn as nn
|
|
43
|
+
import useml
|
|
44
|
+
|
|
45
|
+
useml.init("vault")
|
|
46
|
+
useml.new("mnist-run", auto_focus=True)
|
|
47
|
+
|
|
48
|
+
model = nn.Linear(784, 10)
|
|
49
|
+
useml.track("classifier", model)
|
|
50
|
+
|
|
51
|
+
# ... train ...
|
|
52
|
+
|
|
53
|
+
snap = useml.commit("baseline", val_loss=0.42, accuracy=0.91)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Multiple models, per-model configs
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
useml.track("encoder", encoder, config=encoder_config)
|
|
60
|
+
useml.track("decoder", decoder, config=decoder_config)
|
|
61
|
+
useml.commit("joint training", val_loss=0.18)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Config
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from useml import Config
|
|
68
|
+
|
|
69
|
+
config = Config(
|
|
70
|
+
epochs=30,
|
|
71
|
+
lr=1e-3,
|
|
72
|
+
optimizer="adamw", # "adam" | "adamw" | "sgd"
|
|
73
|
+
loss="cross_entropy", # str key, nn.Module class/instance, or callable
|
|
74
|
+
batch_size=128,
|
|
75
|
+
checkpoint_every=5, # commit every N epochs during useml.train()
|
|
76
|
+
device="auto", # "auto" | "cpu" | "cuda" | "mps"
|
|
77
|
+
)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Dashboard
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
useml.show()
|
|
84
|
+
# --- UseML Dashboard ---
|
|
85
|
+
# Vault: vault/
|
|
86
|
+
# Project: my-experiment (3 snapshots)
|
|
87
|
+
# Tracked: ['classifier']
|
|
88
|
+
# Latest: baseline [snap_20240501_...]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Level-0 API — train in two lines
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import useml
|
|
95
|
+
from useml import Config, Model
|
|
96
|
+
import torch.nn as nn
|
|
97
|
+
|
|
98
|
+
class Net(Model):
|
|
99
|
+
def __init__(self):
|
|
100
|
+
super().__init__()
|
|
101
|
+
self.fc = nn.Linear(784, 10)
|
|
102
|
+
def forward(self, x):
|
|
103
|
+
return self.fc(x.view(x.size(0), -1))
|
|
104
|
+
|
|
105
|
+
useml.init("vault")
|
|
106
|
+
useml.new("mnist-quick", auto_focus=True)
|
|
107
|
+
|
|
108
|
+
history = useml.train(Net, "mnist", config=Config(epochs=10))
|
|
109
|
+
# Built-in datasets: "mnist", "fashion_mnist", "cifar10", "cifar100"
|
|
110
|
+
# HuggingFace: "hf:<dataset_name>"
|
|
111
|
+
# Custom: any torch.utils.data.Dataset
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Loading models
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
# Load latest weights (uses current code)
|
|
118
|
+
model = useml.load("classifier")
|
|
119
|
+
|
|
120
|
+
# Load with full code isolation (archived source + weights)
|
|
121
|
+
useml.mount("\\latest")
|
|
122
|
+
model = useml.load("classifier")
|
|
123
|
+
|
|
124
|
+
# Load from a specific snapshot
|
|
125
|
+
model = useml.load("classifier", _from="\\head~2")
|
|
126
|
+
|
|
127
|
+
# Import archived code directly
|
|
128
|
+
useml.mount("\\latest")
|
|
129
|
+
from useml.workdir.models import Net
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Snapshot tags
|
|
133
|
+
|
|
134
|
+
| Tag | Meaning |
|
|
135
|
+
|---|---|
|
|
136
|
+
| `\latest` | Most recent snapshot |
|
|
137
|
+
| `\head~N` | N snapshots before latest |
|
|
138
|
+
| `\current` / `\workdir` | Unmount (back to live code) |
|
|
139
|
+
| `snap_20240501_...` | Exact snapshot folder name |
|
|
140
|
+
|
|
141
|
+
## Custom loss
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
# String key (built-in)
|
|
145
|
+
Config(loss="cross_entropy") # also: "mse", "bce", "l1"
|
|
146
|
+
|
|
147
|
+
# Class
|
|
148
|
+
Config(loss=nn.HuberLoss)
|
|
149
|
+
|
|
150
|
+
# Instance
|
|
151
|
+
Config(loss=nn.HuberLoss(delta=0.5))
|
|
152
|
+
|
|
153
|
+
# Plain function
|
|
154
|
+
def focal_loss(pred, target): ...
|
|
155
|
+
Config(loss=focal_loss)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The loss source is automatically archived in the snapshot.
|
|
159
|
+
|
|
160
|
+
## Stashing (multi-project workflows)
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
useml.focus("project-a")
|
|
164
|
+
useml.track("model", model_a)
|
|
165
|
+
useml.stash() # park project-a in RAM
|
|
166
|
+
|
|
167
|
+
useml.focus("project-b")
|
|
168
|
+
useml.track("model", model_b)
|
|
169
|
+
useml.commit("run b")
|
|
170
|
+
|
|
171
|
+
useml.focus("project-a") # restore from stash
|
|
172
|
+
useml.commit("run a")
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Error codes
|
|
176
|
+
|
|
177
|
+
All exceptions raised by useml carry a `[UML-NNN]` code. See [ERRORS.md](ERRORS.md) for the full catalog with causes and fixes.
|
|
178
|
+
|
|
179
|
+
## API reference
|
|
180
|
+
|
|
181
|
+
| Function | Description |
|
|
182
|
+
|---|---|
|
|
183
|
+
| `useml.init(vault_path)` | Connect session to a vault directory |
|
|
184
|
+
| `useml.new(name)` | Create and focus a new project |
|
|
185
|
+
| `useml.focus(name)` | Resume an existing project |
|
|
186
|
+
| `useml.track(name, model, config?, optimizer?)` | Register a component |
|
|
187
|
+
| `useml.commit(message, **metrics)` | Save a snapshot |
|
|
188
|
+
| `useml.load(name, _from?)` | Restore a model with weights |
|
|
189
|
+
| `useml.mount(tag)` | Mount a snapshot for workdir imports |
|
|
190
|
+
| `useml.show()` | Print session dashboard |
|
|
191
|
+
| `useml.stash()` | Park current project state in RAM |
|
|
192
|
+
| `useml.projects()` | List all projects in the vault |
|
|
193
|
+
| `useml.train(model_cls, dataset, config?)` | Level-0 training entry point |
|
|
194
|
+
| `useml.debug_imports()` | Inspect workdir importable modules |
|
|
@@ -12,17 +12,19 @@ dev = [
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "useml"
|
|
15
|
-
version = "0.0
|
|
16
|
-
description = "
|
|
15
|
+
version = "0.1.0"
|
|
16
|
+
description = "Minimalist ML experiment versioning for PyTorch — checkpointing, source archiving, and model isolation."
|
|
17
17
|
readme = "README.md"
|
|
18
18
|
requires-python = ">=3.8"
|
|
19
19
|
authors = [{name = "Enzo-py", email = "85191552+Enzo-py@users.noreply.github.com"}]
|
|
20
20
|
dependencies = [
|
|
21
21
|
"torch>=2.0.0",
|
|
22
|
-
"
|
|
23
|
-
"numpy>=1.24.0",
|
|
22
|
+
"pyyaml>=6.0",
|
|
24
23
|
]
|
|
25
24
|
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
include = ["useml*"]
|
|
27
|
+
|
|
26
28
|
[project.urls]
|
|
27
29
|
"Homepage" = "https://github.com/Enzo-py/useml"
|
|
28
30
|
"Bug Tracker" = "https://github.com/Enzo-py/useml/issues"
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import pytest
|
|
5
|
+
import torch
|
|
6
|
+
import useml
|
|
7
|
+
from useml.session.manager import _session, UncommittedSessionError
|
|
8
|
+
|
|
9
|
+
class DummyModel(torch.nn.Module):
|
|
10
|
+
def __init__(self, val=1.0):
|
|
11
|
+
super().__init__()
|
|
12
|
+
self.param = torch.nn.Parameter(torch.ones(1) * val)
|
|
13
|
+
|
|
14
|
+
@pytest.fixture(autouse=True)
|
|
15
|
+
def reset_session():
|
|
16
|
+
"""Wipes the global singleton, including the stash, for each test."""
|
|
17
|
+
_session.vault = None
|
|
18
|
+
_session._project = None
|
|
19
|
+
_session.components = {}
|
|
20
|
+
_session._is_dirty = False
|
|
21
|
+
_session._stash = {} # Ensure stash is cleared
|
|
22
|
+
yield
|
|
23
|
+
|
|
24
|
+
# --- DIRTY STATE & PROTECTION TESTS ---
|
|
25
|
+
|
|
26
|
+
def test_dirty_state_tracking(tmp_path):
|
|
27
|
+
"""Verifies that tracking a component marks the session as dirty."""
|
|
28
|
+
useml.init(vault_path=tmp_path)
|
|
29
|
+
useml.new("proj_a")
|
|
30
|
+
assert _session._is_dirty is False
|
|
31
|
+
|
|
32
|
+
useml.track("model", DummyModel())
|
|
33
|
+
assert _session._is_dirty is True
|
|
34
|
+
|
|
35
|
+
useml.commit("First save")
|
|
36
|
+
assert _session._is_dirty is False
|
|
37
|
+
|
|
38
|
+
def test_error_uncommitted_switch(tmp_path):
|
|
39
|
+
"""Verifies that switching focus without committing or stashing raises an error."""
|
|
40
|
+
useml.init(vault_path=tmp_path)
|
|
41
|
+
useml.new("proj_a")
|
|
42
|
+
useml.track("m1", DummyModel())
|
|
43
|
+
|
|
44
|
+
# Attempting to switch should fail
|
|
45
|
+
with pytest.raises(UncommittedSessionError, match="unsaved changes"):
|
|
46
|
+
useml.focus("proj_b")
|
|
47
|
+
|
|
48
|
+
# Verification: we are still on proj_a and dirty
|
|
49
|
+
assert _session.project.path.name == "proj_a"
|
|
50
|
+
assert "m1" in _session.components
|
|
51
|
+
|
|
52
|
+
def test_force_focus_discards_changes(tmp_path):
|
|
53
|
+
"""Verifies that focus(force=True) bypasses the dirty check and wipes RAM."""
|
|
54
|
+
useml.init(vault_path=tmp_path)
|
|
55
|
+
useml.new("proj_a")
|
|
56
|
+
useml.track("m1", DummyModel())
|
|
57
|
+
|
|
58
|
+
# Forced switch
|
|
59
|
+
useml.focus("proj_b", force=True)
|
|
60
|
+
|
|
61
|
+
assert _session.project.path.name == "proj_b"
|
|
62
|
+
assert len(_session.components) == 0 # RAM wiped
|
|
63
|
+
assert _session._is_dirty is False
|
|
64
|
+
|
|
65
|
+
# --- STASH LOGIC TESTS ---
|
|
66
|
+
|
|
67
|
+
def test_stash_and_restore_flow(tmp_path):
|
|
68
|
+
"""Tests the full stash cycle: track A -> stash -> work B -> focus A (restore)."""
|
|
69
|
+
useml.init(vault_path=tmp_path)
|
|
70
|
+
|
|
71
|
+
# 1. Start Project A
|
|
72
|
+
useml.new("proj_a")
|
|
73
|
+
model_a = DummyModel(1.0)
|
|
74
|
+
useml.track("net_a", model_a)
|
|
75
|
+
assert _session._is_dirty is True
|
|
76
|
+
|
|
77
|
+
# 2. Stash A
|
|
78
|
+
useml.stash()
|
|
79
|
+
assert _session._project is None
|
|
80
|
+
assert len(_session.components) == 0
|
|
81
|
+
assert "proj_a" in _session._stash
|
|
82
|
+
|
|
83
|
+
# 3. Work on Project B
|
|
84
|
+
useml.new("proj_b")
|
|
85
|
+
useml.track("net_b", DummyModel(2.0))
|
|
86
|
+
useml.commit("Save B")
|
|
87
|
+
|
|
88
|
+
# 4. Return to Project A (should auto-restore from stash)
|
|
89
|
+
useml.focus("proj_a")
|
|
90
|
+
assert _session.project.path.name == "proj_a"
|
|
91
|
+
assert "net_a" in _session.components
|
|
92
|
+
assert _session._is_dirty is True # Still dirty because we never committed A
|
|
93
|
+
assert _session.components["net_a"].model.param.item() == 1.0
|
|
94
|
+
|
|
95
|
+
def test_stash_overwrites_on_re_stash(tmp_path):
|
|
96
|
+
"""Verifies that stashing the same project again updates the stashed state."""
|
|
97
|
+
useml.init(vault_path=tmp_path)
|
|
98
|
+
useml.new("proj_a")
|
|
99
|
+
|
|
100
|
+
useml.track("m", DummyModel(1.0))
|
|
101
|
+
useml.stash()
|
|
102
|
+
|
|
103
|
+
useml.focus("proj_a")
|
|
104
|
+
useml.track("m_new", DummyModel(2.0)) # Added a new component
|
|
105
|
+
useml.stash()
|
|
106
|
+
|
|
107
|
+
assert len(_session._stash["proj_a"].components) == 2
|
|
108
|
+
|
|
109
|
+
# --- DASHBOARD UPDATES ---
|
|
110
|
+
|
|
111
|
+
def test_show_dirty_warning(tmp_path, capsys):
|
|
112
|
+
"""Ensures show() displays a warning when the session is dirty."""
|
|
113
|
+
useml.init(vault_path=tmp_path)
|
|
114
|
+
useml.new("dirty_proj")
|
|
115
|
+
useml.track("m", DummyModel())
|
|
116
|
+
# No commit — _is_dirty stays True
|
|
117
|
+
useml.show()
|
|
118
|
+
out = capsys.readouterr().out
|
|
119
|
+
assert "WARNING" in out
|
|
120
|
+
assert "unsaved components" in out
|
|
121
|
+
|
|
122
|
+
# --- REGRESSION TESTS ---
|
|
123
|
+
|
|
124
|
+
def test_focus_same_project_is_no_op(tmp_path):
|
|
125
|
+
"""Focusing on the current project should not trigger dirty errors or wipe RAM."""
|
|
126
|
+
useml.init(vault_path=tmp_path)
|
|
127
|
+
useml.new("proj_a")
|
|
128
|
+
useml.track("m", DummyModel())
|
|
129
|
+
|
|
130
|
+
# This should NOT raise UncommittedSessionError
|
|
131
|
+
useml.focus("proj_a")
|
|
132
|
+
assert len(_session.components) == 1
|
|
133
|
+
|
|
134
|
+
def test_session_mount_isolation(tmp_path):
|
|
135
|
+
import os
|
|
136
|
+
import sys
|
|
137
|
+
import importlib
|
|
138
|
+
import torch
|
|
139
|
+
from conftest import ModuleManager, FileManager
|
|
140
|
+
|
|
141
|
+
project_dir = tmp_path / "app"
|
|
142
|
+
project_dir.mkdir()
|
|
143
|
+
vault_dir = tmp_path / "vault"
|
|
144
|
+
|
|
145
|
+
os.chdir(project_dir)
|
|
146
|
+
sys.path.insert(0, str(project_dir))
|
|
147
|
+
|
|
148
|
+
try:
|
|
149
|
+
useml.init(vault_path=vault_dir)
|
|
150
|
+
useml.new("iso_test")
|
|
151
|
+
|
|
152
|
+
module_path = project_dir / "net.py"
|
|
153
|
+
|
|
154
|
+
# --- VERSION 1 ---
|
|
155
|
+
FileManager.write_simple_model(module_path, "v1")
|
|
156
|
+
net = ModuleManager.reload_fresh("net", project_dir)
|
|
157
|
+
assert net.Model.VERSION == "v1"
|
|
158
|
+
|
|
159
|
+
model = net.Model()
|
|
160
|
+
useml.track("m", model)
|
|
161
|
+
|
|
162
|
+
model.w.data.fill_(1.0)
|
|
163
|
+
snap_v1 = useml.commit("v1")
|
|
164
|
+
|
|
165
|
+
# --- VERSION 2 (code + poids) ---
|
|
166
|
+
FileManager.write_simple_model(module_path, "v2")
|
|
167
|
+
net = ModuleManager.reload_fresh("net", project_dir)
|
|
168
|
+
|
|
169
|
+
model.w.data.fill_(2.0)
|
|
170
|
+
useml.track("m", model)
|
|
171
|
+
useml.commit("v2")
|
|
172
|
+
|
|
173
|
+
# -------------------------
|
|
174
|
+
# TEST LOAD CURRENT
|
|
175
|
+
# -------------------------
|
|
176
|
+
m_current = useml.load("m")
|
|
177
|
+
assert m_current.w.item() == 2.0
|
|
178
|
+
assert net.Model.VERSION == "v2"
|
|
179
|
+
|
|
180
|
+
# -------------------------
|
|
181
|
+
# TEST LOAD SNAPSHOT
|
|
182
|
+
# -------------------------
|
|
183
|
+
useml.mount("\\latest")
|
|
184
|
+
|
|
185
|
+
m_snap = useml.load("m")
|
|
186
|
+
assert m_snap.w.item() == 2.0 # latest snapshot
|
|
187
|
+
|
|
188
|
+
useml.mount("\\head~1")
|
|
189
|
+
|
|
190
|
+
m_old = useml.load("m")
|
|
191
|
+
assert m_old.w.item() == 1.0
|
|
192
|
+
|
|
193
|
+
# -------------------------
|
|
194
|
+
# TEST CODE ISOLATION
|
|
195
|
+
# -------------------------
|
|
196
|
+
source_dir = Path(_session._mounted_sys_path)
|
|
197
|
+
snap_file = list(source_dir.rglob("net.py"))[0]
|
|
198
|
+
snap_code = snap_file.read_text()
|
|
199
|
+
|
|
200
|
+
assert "VERSION = 'v1'" in snap_code
|
|
201
|
+
|
|
202
|
+
# back to current
|
|
203
|
+
useml.mount("\\workdir")
|
|
204
|
+
|
|
205
|
+
net = ModuleManager.reload_fresh("net", project_dir)
|
|
206
|
+
assert net.Model.VERSION == "v2"
|
|
207
|
+
|
|
208
|
+
finally:
|
|
209
|
+
ModuleManager.clear("net", "useml.workdir.net")
|
|
210
|
+
if str(project_dir) in sys.path:
|
|
211
|
+
sys.path.remove(str(project_dir))
|