DepthML 0.1.0__tar.gz → 0.3.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.
- depthml-0.3.0/DepthML.egg-info/PKG-INFO +225 -0
- depthml-0.3.0/DepthML.egg-info/SOURCES.txt +69 -0
- depthml-0.3.0/DepthML.egg-info/requires.txt +2 -0
- depthml-0.3.0/DepthML.egg-info/top_level.txt +1 -0
- {depthml-0.1.0 → depthml-0.3.0}/LICENSE +1 -1
- depthml-0.3.0/PKG-INFO +225 -0
- depthml-0.3.0/README.md +213 -0
- depthml-0.3.0/pyproject.toml +16 -0
- depthml-0.3.0/setup.py +11 -0
- depthml-0.3.0/src/DepthML/__init__.py +18 -0
- depthml-0.3.0/src/DepthML/_model_config.py +79 -0
- depthml-0.3.0/src/DepthML/activations/__init__.py +2 -0
- depthml-0.3.0/src/DepthML/activations/base_activation.py +33 -0
- depthml-0.3.0/src/DepthML/activations/common_activations.py +303 -0
- depthml-0.3.0/src/DepthML/block.py +711 -0
- depthml-0.3.0/src/DepthML/checkpoint.py +208 -0
- depthml-0.3.0/src/DepthML/data/__init__.py +2 -0
- depthml-0.3.0/src/DepthML/data/batching.py +117 -0
- depthml-0.3.0/src/DepthML/data/loader.py +113 -0
- depthml-0.3.0/src/DepthML/enums/__init__.py +1 -0
- depthml-0.3.0/src/DepthML/enums/reduction.py +8 -0
- depthml-0.3.0/src/DepthML/initializers/__init__.py +5 -0
- depthml-0.3.0/src/DepthML/initializers/base_initializer.py +82 -0
- depthml-0.3.0/src/DepthML/initializers/glorot.py +119 -0
- depthml-0.3.0/src/DepthML/initializers/kaiming.py +154 -0
- depthml-0.3.0/src/DepthML/initializers/ones.py +54 -0
- depthml-0.3.0/src/DepthML/initializers/zeros.py +54 -0
- depthml-0.3.0/src/DepthML/layers/__init__.py +13 -0
- depthml-0.3.0/src/DepthML/layers/attention.py +237 -0
- depthml-0.3.0/src/DepthML/layers/base_layer.py +16 -0
- depthml-0.3.0/src/DepthML/layers/batch_normalization.py +181 -0
- depthml-0.3.0/src/DepthML/layers/conv2d.py +262 -0
- depthml-0.3.0/src/DepthML/layers/dropout.py +91 -0
- depthml-0.3.0/src/DepthML/layers/embedding.py +108 -0
- depthml-0.3.0/src/DepthML/layers/flatten.py +120 -0
- depthml-0.3.0/src/DepthML/layers/global_pooling.py +77 -0
- depthml-0.3.0/src/DepthML/layers/layer_norm.py +120 -0
- depthml-0.3.0/src/DepthML/layers/linear.py +141 -0
- depthml-0.3.0/src/DepthML/layers/pooling.py +204 -0
- depthml-0.3.0/src/DepthML/layers/recurrent.py +229 -0
- depthml-0.3.0/src/DepthML/layers/transformer_encoder.py +161 -0
- depthml-0.3.0/src/DepthML/losses/__init__.py +5 -0
- depthml-0.3.0/src/DepthML/losses/base_loss.py +38 -0
- depthml-0.3.0/src/DepthML/losses/categorical_cross_entropy.py +83 -0
- depthml-0.3.0/src/DepthML/losses/cross_entropy_loss.py +166 -0
- depthml-0.3.0/src/DepthML/losses/mean_squared_error.py +115 -0
- depthml-0.3.0/src/DepthML/losses/softmax_categorical_cross_entropy.py +163 -0
- depthml-0.3.0/src/DepthML/metrics.py +134 -0
- depthml-0.3.0/src/DepthML/model_io.py +272 -0
- depthml-0.3.0/src/DepthML/models/__init__.py +2 -0
- depthml-0.3.0/src/DepthML/models/base_model.py +190 -0
- depthml-0.3.0/src/DepthML/models/sequential.py +176 -0
- depthml-0.3.0/src/DepthML/onnx_export.py +174 -0
- depthml-0.3.0/src/DepthML/optimizers/__init__.py +6 -0
- depthml-0.3.0/src/DepthML/optimizers/adam.py +122 -0
- depthml-0.3.0/src/DepthML/optimizers/adamw.py +37 -0
- depthml-0.3.0/src/DepthML/optimizers/base_optimizer.py +335 -0
- depthml-0.3.0/src/DepthML/optimizers/rmsprop.py +78 -0
- depthml-0.3.0/src/DepthML/optimizers/schedulers.py +168 -0
- depthml-0.3.0/src/DepthML/optimizers/sgd.py +74 -0
- depthml-0.3.0/src/DepthML/typing.py +20 -0
- depthml-0.3.0/src/DepthML/utils/__init__.py +3 -0
- depthml-0.3.0/src/DepthML/utils/_grad.py +15 -0
- depthml-0.3.0/src/DepthML/utils/_mean.py +39 -0
- depthml-0.3.0/src/DepthML/utils/_reshape.py +18 -0
- depthml-0.3.0/src/DepthML/utils/batch.py +71 -0
- depthml-0.3.0/src/DepthML/utils/clip_grad_norm.py +86 -0
- depthml-0.3.0/src/DepthML/utils/get_xp.py +14 -0
- depthml-0.3.0/src/DepthML/utils/im2col.py +252 -0
- depthml-0.1.0/PKG-INFO +0 -8
- depthml-0.1.0/README.md +0 -0
- depthml-0.1.0/pyproject.toml +0 -15
- depthml-0.1.0/src/DepthML/__init__.py +0 -5
- depthml-0.1.0/src/DepthML/components/__init__.py +0 -3
- depthml-0.1.0/src/DepthML/components/activation/__init__.py +0 -3
- depthml-0.1.0/src/DepthML/components/activation/base_activation.py +0 -19
- depthml-0.1.0/src/DepthML/components/activation/identity.py +0 -27
- depthml-0.1.0/src/DepthML/components/activation/leaky_relu.py +0 -32
- depthml-0.1.0/src/DepthML/components/activation/relu.py +0 -28
- depthml-0.1.0/src/DepthML/components/base_component.py +0 -19
- depthml-0.1.0/src/DepthML/components/layer/__init__.py +0 -1
- depthml-0.1.0/src/DepthML/components/layer/affine.py +0 -25
- depthml-0.1.0/src/DepthML/components/layer/base_layer.py +0 -23
- depthml-0.1.0/src/DepthML/components/loss/__init__.py +0 -1
- depthml-0.1.0/src/DepthML/components/loss/base_loss.py +0 -24
- depthml-0.1.0/src/DepthML/components/loss/mean_squared_error.py +0 -14
- depthml-0.1.0/src/DepthML/initializers/__init__.py +0 -4
- depthml-0.1.0/src/DepthML/initializers/base_initializer.py +0 -33
- depthml-0.1.0/src/DepthML/initializers/he.py +0 -16
- depthml-0.1.0/src/DepthML/initializers/uniform.py +0 -15
- depthml-0.1.0/src/DepthML/initializers/xavier.py +0 -16
- depthml-0.1.0/src/DepthML/models/__init__.py +0 -1
- depthml-0.1.0/src/DepthML/models/base_model.py +0 -23
- depthml-0.1.0/src/DepthML/models/stack.py +0 -57
- depthml-0.1.0/src/DepthML/typing.py +0 -19
- depthml-0.1.0/src/DepthML.egg-info/PKG-INFO +0 -8
- depthml-0.1.0/src/DepthML.egg-info/SOURCES.txt +0 -31
- depthml-0.1.0/src/DepthML.egg-info/requires.txt +0 -2
- depthml-0.1.0/src/DepthML.egg-info/top_level.txt +0 -1
- {depthml-0.1.0/src → depthml-0.3.0}/DepthML.egg-info/dependency_links.txt +0 -0
- {depthml-0.1.0 → depthml-0.3.0}/setup.cfg +0 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: DepthML
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Neural network layers, losses, and optimizers for DepthTensor
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: numpy
|
|
10
|
+
Requires-Dist: depthtensor<4,>=3.0.1
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# DepthML
|
|
14
|
+
|
|
15
|
+
Neural network layers, losses, and optimizers for [DepthTensor](https://github.com/l-h-ha/DepthTensor).
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
python -m pip install "depthml==0.3.0"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
DepthML 0.3.0 requires DepthTensor 3.0.1 or later in the 3.x series. To install both from sibling checkouts, run this from the DepthML root.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
python -m pip install ../DepthTensor .
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Use `device="cuda"` for the model and tensors after [setting up DepthTensor for CUDA](https://github.com/l-h-ha/DepthTensor#cpu--cuda).
|
|
30
|
+
|
|
31
|
+
## Training
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from depthml import Linear, MeanSquaredError, ReLU, SGD, Sequential, Tensor, batches
|
|
35
|
+
|
|
36
|
+
device = "cpu"
|
|
37
|
+
model = Sequential(Linear(8), ReLU(), Linear(1), device=device)
|
|
38
|
+
model.build((2,))
|
|
39
|
+
|
|
40
|
+
x = Tensor([[0., 1.], [1., 0.]], device=device)
|
|
41
|
+
y = Tensor([[1.], [0.]], device=device)
|
|
42
|
+
loss_fn = MeanSquaredError()
|
|
43
|
+
optim = SGD(model.parameters(), learning_rate=0.1)
|
|
44
|
+
|
|
45
|
+
for _ in range(20):
|
|
46
|
+
optim.zero_grad()
|
|
47
|
+
loss = loss_fn(y, model(x))
|
|
48
|
+
loss.backward()
|
|
49
|
+
optim.step()
|
|
50
|
+
|
|
51
|
+
print(model.evaluate(batches(x, y, batch_size=2), loss_fn))
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Pass the input shape without the batch dimension to `build`. Use `shuffle=True` with `batches` for training. `evaluate` uses evaluation mode for its forward passes.
|
|
55
|
+
Pass `reduction=Reduction.SUM` to `evaluate` if your custom loss returns a sum.
|
|
56
|
+
|
|
57
|
+
## Examples
|
|
58
|
+
|
|
59
|
+
Run these from the repository root.
|
|
60
|
+
|
|
61
|
+
| Example | Command |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| MNIST MLP on CPU | `python -m example.mlp_mnist` |
|
|
64
|
+
| MNIST CNN on CUDA | `python -m example.mnist_conv2d` |
|
|
65
|
+
| Tiny Transformer that generates text | `python -m example.transformer_text` |
|
|
66
|
+
| GRU that forecasts the next time step | `python -m example.gru_forecast` |
|
|
67
|
+
|
|
68
|
+
The Transformer and GRU examples run on CPU with built-in data.
|
|
69
|
+
|
|
70
|
+
## Sequence layers
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from depthml import Embedding, GRU, Linear, Sequential, Tensor, TransformerEncoderLayer, no_grad
|
|
74
|
+
|
|
75
|
+
device = "cpu"
|
|
76
|
+
model = Sequential(
|
|
77
|
+
Embedding(100, 16),
|
|
78
|
+
TransformerEncoderLayer(16, 4, dim_feedforward=32),
|
|
79
|
+
GRU(16, return_sequences=False),
|
|
80
|
+
Linear(2),
|
|
81
|
+
)
|
|
82
|
+
model.build((3,))
|
|
83
|
+
model.to(device)
|
|
84
|
+
model.eval()
|
|
85
|
+
|
|
86
|
+
tokens = Tensor([[4, 8, 15], [16, 23, 42]], device=device)
|
|
87
|
+
with no_grad():
|
|
88
|
+
scores = model(tokens)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Attention uses self-attention when given one tensor. `RNN`, `GRU`, and `LSTM` take `(batch, steps, features)` and return all steps by default. Call a recurrent layer with `return_state=True` to get its final state too.
|
|
92
|
+
|
|
93
|
+
Set `device = "cuda"` to run on CUDA. `model.to()` moves model parameters and buffers. `no_grad()` pauses gradient recording. Both use the current DepthTensor checkout.
|
|
94
|
+
|
|
95
|
+
`train()` and `eval()` set the mode of nested layers. `named_modules()` and `named_buffers()` list them by path.
|
|
96
|
+
|
|
97
|
+
## Data and training tools
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from depthml import (
|
|
101
|
+
CrossEntropyLoss, DataLoader, Linear, SGD, Sequential, StepLR,
|
|
102
|
+
TensorDataset, accuracy,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
data = TensorDataset([[0., 1.], [1., 0.]], [0, 1])
|
|
106
|
+
loader = DataLoader(data, batch_size=2, shuffle=True)
|
|
107
|
+
model = Sequential(Linear(2))
|
|
108
|
+
model.build((2,))
|
|
109
|
+
optim = SGD(model.parameters(), learning_rate=0.1)
|
|
110
|
+
schedule = StepLR(optim, step_size=10)
|
|
111
|
+
loss_fn = CrossEntropyLoss()
|
|
112
|
+
|
|
113
|
+
for x, labels in loader:
|
|
114
|
+
optim.zero_grad()
|
|
115
|
+
logits = model(x)
|
|
116
|
+
loss_fn(labels, logits).backward()
|
|
117
|
+
optim.step()
|
|
118
|
+
schedule.step()
|
|
119
|
+
print(accuracy(labels, logits))
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Use `CosineAnnealingLR(optim, total_steps=100)` for cosine decay. For regression, use `mae` or `mse`.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from depthml import DataLoader, pad_sequence
|
|
126
|
+
|
|
127
|
+
loader = DataLoader([[1, 2, 3], [4]], batch_size=2, collate_fn=pad_sequence)
|
|
128
|
+
tokens, padding_mask = next(iter(loader))
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Pass `padding_mask` as `key_padding_mask` to attention or a Transformer encoder layer. For token labels, pad with `padding_value=-100` and set `ignore_index=-100` in the loss and accuracy.
|
|
132
|
+
|
|
133
|
+
## Save and load a model
|
|
134
|
+
|
|
135
|
+
Save a built `Sequential` model with built-in layers and activations.
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from depthml import Linear, Sequential, load_model, save_model
|
|
139
|
+
|
|
140
|
+
model = Sequential(Linear(1))
|
|
141
|
+
model.build((2,))
|
|
142
|
+
save_model("model.npz", model)
|
|
143
|
+
|
|
144
|
+
restored = load_model("model.npz").eval()
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`load_model()` builds the model from the file and restores its weights and buffers. It loads on CPU by default. Pass `device="cuda"` to load on CUDA.
|
|
148
|
+
|
|
149
|
+
## Checkpoints
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from depthml import AdamW, Linear, Sequential, StepLR, load_checkpoint, save_checkpoint
|
|
153
|
+
|
|
154
|
+
backbone, head = Linear(8), Linear(2)
|
|
155
|
+
model = Sequential(backbone, head)
|
|
156
|
+
model.build((4,))
|
|
157
|
+
optim = AdamW([
|
|
158
|
+
{"params": backbone.parameters(), "learning_rate": 1e-4},
|
|
159
|
+
{"params": head.parameters(), "weight_decay": 0.0},
|
|
160
|
+
], learning_rate=1e-3)
|
|
161
|
+
schedule = StepLR(optim, step_size=10)
|
|
162
|
+
|
|
163
|
+
save_checkpoint("checkpoint.npz", model, scheduler=schedule)
|
|
164
|
+
load_checkpoint("checkpoint.npz", model, scheduler=schedule)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Build the model before loading. Pass an optimizer to save its settings and buffers. `scheduler=` saves the scheduler and its optimizer. To resume training, use the same model, optimizer, and parameter groups.
|
|
168
|
+
Use `register_parameter(name, tensor)` for weights added after `build()`. Use `register_buffer(name, tensor)` for custom buffers.
|
|
169
|
+
|
|
170
|
+
## ONNX export
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
python -m pip install onnx onnxruntime
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Use the trained model from the [training example](#training).
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
import numpy as np
|
|
180
|
+
import onnxruntime as ort
|
|
181
|
+
from depthml import Tensor, export_onnx, no_grad
|
|
182
|
+
|
|
183
|
+
model.eval()
|
|
184
|
+
export_onnx("model.onnx", model)
|
|
185
|
+
|
|
186
|
+
x = np.array([[0., 1.], [1., 0.]], dtype=np.float32)
|
|
187
|
+
session = ort.InferenceSession("model.onnx", providers=["CPUExecutionProvider"])
|
|
188
|
+
actual, = session.run(None, {session.get_inputs()[0].name: x})
|
|
189
|
+
with no_grad():
|
|
190
|
+
expected = model(Tensor(x)).data
|
|
191
|
+
|
|
192
|
+
np.testing.assert_allclose(actual, expected, rtol=1e-5, atol=1e-6)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Export supports float32 `Sequential` models with `Linear`, `Conv2d`, `Flatten`, `MaxPooling2D`, `GlobalAveragePooling2D`, `Dropout`, `ReLU`, `LeakyReLU`, `ELU`, `Sigmoid`, `Tanh`, and `Softmax`. Batch size can vary, and the sample shape comes from `build()`.
|
|
196
|
+
|
|
197
|
+
## CPU benchmark
|
|
198
|
+
|
|
199
|
+
Training step on 64 samples with a 128 → 64 → 10 MLP, ReLU, MSE, and SGD. Median and range across six runs, in ms.
|
|
200
|
+
|
|
201
|
+
| DepthML | PyTorch | TensorFlow |
|
|
202
|
+
| ---: | ---: | ---: |
|
|
203
|
+
| 3.02 (1.37–3.94) | **0.64 (0.43–0.90)** | 13.42 (8.22–16.68) |
|
|
204
|
+
|
|
205
|
+
Intel Core i5-11400H, Windows 11, Python 3.11.15. DepthML 0.3.0, DepthTensor 3.0.0, PyTorch 2.12.1+cpu, TensorFlow 2.21.0.
|
|
206
|
+
|
|
207
|
+
```sh
|
|
208
|
+
python -m pip install torch tensorflow-cpu
|
|
209
|
+
python benchmarks/run.py
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Build
|
|
213
|
+
|
|
214
|
+
```sh
|
|
215
|
+
python -m pip install build pytest
|
|
216
|
+
python -m build
|
|
217
|
+
python -m pip install dist/depthml-0.3.0-py3-none-any.whl
|
|
218
|
+
python -m pytest -q
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
With PyTorch installed, run `python -m pytest -q tests/numerical/test_pytorch_comparison.py` to compare Transformer outputs, gradients, and an AdamW update.
|
|
222
|
+
|
|
223
|
+
## License
|
|
224
|
+
|
|
225
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.py
|
|
5
|
+
DepthML.egg-info/PKG-INFO
|
|
6
|
+
DepthML.egg-info/SOURCES.txt
|
|
7
|
+
DepthML.egg-info/dependency_links.txt
|
|
8
|
+
DepthML.egg-info/requires.txt
|
|
9
|
+
DepthML.egg-info/top_level.txt
|
|
10
|
+
src/DepthML/__init__.py
|
|
11
|
+
src/DepthML/_model_config.py
|
|
12
|
+
src/DepthML/block.py
|
|
13
|
+
src/DepthML/checkpoint.py
|
|
14
|
+
src/DepthML/metrics.py
|
|
15
|
+
src/DepthML/model_io.py
|
|
16
|
+
src/DepthML/onnx_export.py
|
|
17
|
+
src/DepthML/typing.py
|
|
18
|
+
src/DepthML/activations/__init__.py
|
|
19
|
+
src/DepthML/activations/base_activation.py
|
|
20
|
+
src/DepthML/activations/common_activations.py
|
|
21
|
+
src/DepthML/data/__init__.py
|
|
22
|
+
src/DepthML/data/batching.py
|
|
23
|
+
src/DepthML/data/loader.py
|
|
24
|
+
src/DepthML/enums/__init__.py
|
|
25
|
+
src/DepthML/enums/reduction.py
|
|
26
|
+
src/DepthML/initializers/__init__.py
|
|
27
|
+
src/DepthML/initializers/base_initializer.py
|
|
28
|
+
src/DepthML/initializers/glorot.py
|
|
29
|
+
src/DepthML/initializers/kaiming.py
|
|
30
|
+
src/DepthML/initializers/ones.py
|
|
31
|
+
src/DepthML/initializers/zeros.py
|
|
32
|
+
src/DepthML/layers/__init__.py
|
|
33
|
+
src/DepthML/layers/attention.py
|
|
34
|
+
src/DepthML/layers/base_layer.py
|
|
35
|
+
src/DepthML/layers/batch_normalization.py
|
|
36
|
+
src/DepthML/layers/conv2d.py
|
|
37
|
+
src/DepthML/layers/dropout.py
|
|
38
|
+
src/DepthML/layers/embedding.py
|
|
39
|
+
src/DepthML/layers/flatten.py
|
|
40
|
+
src/DepthML/layers/global_pooling.py
|
|
41
|
+
src/DepthML/layers/layer_norm.py
|
|
42
|
+
src/DepthML/layers/linear.py
|
|
43
|
+
src/DepthML/layers/pooling.py
|
|
44
|
+
src/DepthML/layers/recurrent.py
|
|
45
|
+
src/DepthML/layers/transformer_encoder.py
|
|
46
|
+
src/DepthML/losses/__init__.py
|
|
47
|
+
src/DepthML/losses/base_loss.py
|
|
48
|
+
src/DepthML/losses/categorical_cross_entropy.py
|
|
49
|
+
src/DepthML/losses/cross_entropy_loss.py
|
|
50
|
+
src/DepthML/losses/mean_squared_error.py
|
|
51
|
+
src/DepthML/losses/softmax_categorical_cross_entropy.py
|
|
52
|
+
src/DepthML/models/__init__.py
|
|
53
|
+
src/DepthML/models/base_model.py
|
|
54
|
+
src/DepthML/models/sequential.py
|
|
55
|
+
src/DepthML/optimizers/__init__.py
|
|
56
|
+
src/DepthML/optimizers/adam.py
|
|
57
|
+
src/DepthML/optimizers/adamw.py
|
|
58
|
+
src/DepthML/optimizers/base_optimizer.py
|
|
59
|
+
src/DepthML/optimizers/rmsprop.py
|
|
60
|
+
src/DepthML/optimizers/schedulers.py
|
|
61
|
+
src/DepthML/optimizers/sgd.py
|
|
62
|
+
src/DepthML/utils/__init__.py
|
|
63
|
+
src/DepthML/utils/_grad.py
|
|
64
|
+
src/DepthML/utils/_mean.py
|
|
65
|
+
src/DepthML/utils/_reshape.py
|
|
66
|
+
src/DepthML/utils/batch.py
|
|
67
|
+
src/DepthML/utils/clip_grad_norm.py
|
|
68
|
+
src/DepthML/utils/get_xp.py
|
|
69
|
+
src/DepthML/utils/im2col.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
depthml
|
depthml-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: DepthML
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Neural network layers, losses, and optimizers for DepthTensor
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: numpy
|
|
10
|
+
Requires-Dist: depthtensor<4,>=3.0.1
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# DepthML
|
|
14
|
+
|
|
15
|
+
Neural network layers, losses, and optimizers for [DepthTensor](https://github.com/l-h-ha/DepthTensor).
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
python -m pip install "depthml==0.3.0"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
DepthML 0.3.0 requires DepthTensor 3.0.1 or later in the 3.x series. To install both from sibling checkouts, run this from the DepthML root.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
python -m pip install ../DepthTensor .
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Use `device="cuda"` for the model and tensors after [setting up DepthTensor for CUDA](https://github.com/l-h-ha/DepthTensor#cpu--cuda).
|
|
30
|
+
|
|
31
|
+
## Training
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from depthml import Linear, MeanSquaredError, ReLU, SGD, Sequential, Tensor, batches
|
|
35
|
+
|
|
36
|
+
device = "cpu"
|
|
37
|
+
model = Sequential(Linear(8), ReLU(), Linear(1), device=device)
|
|
38
|
+
model.build((2,))
|
|
39
|
+
|
|
40
|
+
x = Tensor([[0., 1.], [1., 0.]], device=device)
|
|
41
|
+
y = Tensor([[1.], [0.]], device=device)
|
|
42
|
+
loss_fn = MeanSquaredError()
|
|
43
|
+
optim = SGD(model.parameters(), learning_rate=0.1)
|
|
44
|
+
|
|
45
|
+
for _ in range(20):
|
|
46
|
+
optim.zero_grad()
|
|
47
|
+
loss = loss_fn(y, model(x))
|
|
48
|
+
loss.backward()
|
|
49
|
+
optim.step()
|
|
50
|
+
|
|
51
|
+
print(model.evaluate(batches(x, y, batch_size=2), loss_fn))
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Pass the input shape without the batch dimension to `build`. Use `shuffle=True` with `batches` for training. `evaluate` uses evaluation mode for its forward passes.
|
|
55
|
+
Pass `reduction=Reduction.SUM` to `evaluate` if your custom loss returns a sum.
|
|
56
|
+
|
|
57
|
+
## Examples
|
|
58
|
+
|
|
59
|
+
Run these from the repository root.
|
|
60
|
+
|
|
61
|
+
| Example | Command |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| MNIST MLP on CPU | `python -m example.mlp_mnist` |
|
|
64
|
+
| MNIST CNN on CUDA | `python -m example.mnist_conv2d` |
|
|
65
|
+
| Tiny Transformer that generates text | `python -m example.transformer_text` |
|
|
66
|
+
| GRU that forecasts the next time step | `python -m example.gru_forecast` |
|
|
67
|
+
|
|
68
|
+
The Transformer and GRU examples run on CPU with built-in data.
|
|
69
|
+
|
|
70
|
+
## Sequence layers
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from depthml import Embedding, GRU, Linear, Sequential, Tensor, TransformerEncoderLayer, no_grad
|
|
74
|
+
|
|
75
|
+
device = "cpu"
|
|
76
|
+
model = Sequential(
|
|
77
|
+
Embedding(100, 16),
|
|
78
|
+
TransformerEncoderLayer(16, 4, dim_feedforward=32),
|
|
79
|
+
GRU(16, return_sequences=False),
|
|
80
|
+
Linear(2),
|
|
81
|
+
)
|
|
82
|
+
model.build((3,))
|
|
83
|
+
model.to(device)
|
|
84
|
+
model.eval()
|
|
85
|
+
|
|
86
|
+
tokens = Tensor([[4, 8, 15], [16, 23, 42]], device=device)
|
|
87
|
+
with no_grad():
|
|
88
|
+
scores = model(tokens)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Attention uses self-attention when given one tensor. `RNN`, `GRU`, and `LSTM` take `(batch, steps, features)` and return all steps by default. Call a recurrent layer with `return_state=True` to get its final state too.
|
|
92
|
+
|
|
93
|
+
Set `device = "cuda"` to run on CUDA. `model.to()` moves model parameters and buffers. `no_grad()` pauses gradient recording. Both use the current DepthTensor checkout.
|
|
94
|
+
|
|
95
|
+
`train()` and `eval()` set the mode of nested layers. `named_modules()` and `named_buffers()` list them by path.
|
|
96
|
+
|
|
97
|
+
## Data and training tools
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from depthml import (
|
|
101
|
+
CrossEntropyLoss, DataLoader, Linear, SGD, Sequential, StepLR,
|
|
102
|
+
TensorDataset, accuracy,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
data = TensorDataset([[0., 1.], [1., 0.]], [0, 1])
|
|
106
|
+
loader = DataLoader(data, batch_size=2, shuffle=True)
|
|
107
|
+
model = Sequential(Linear(2))
|
|
108
|
+
model.build((2,))
|
|
109
|
+
optim = SGD(model.parameters(), learning_rate=0.1)
|
|
110
|
+
schedule = StepLR(optim, step_size=10)
|
|
111
|
+
loss_fn = CrossEntropyLoss()
|
|
112
|
+
|
|
113
|
+
for x, labels in loader:
|
|
114
|
+
optim.zero_grad()
|
|
115
|
+
logits = model(x)
|
|
116
|
+
loss_fn(labels, logits).backward()
|
|
117
|
+
optim.step()
|
|
118
|
+
schedule.step()
|
|
119
|
+
print(accuracy(labels, logits))
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Use `CosineAnnealingLR(optim, total_steps=100)` for cosine decay. For regression, use `mae` or `mse`.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from depthml import DataLoader, pad_sequence
|
|
126
|
+
|
|
127
|
+
loader = DataLoader([[1, 2, 3], [4]], batch_size=2, collate_fn=pad_sequence)
|
|
128
|
+
tokens, padding_mask = next(iter(loader))
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Pass `padding_mask` as `key_padding_mask` to attention or a Transformer encoder layer. For token labels, pad with `padding_value=-100` and set `ignore_index=-100` in the loss and accuracy.
|
|
132
|
+
|
|
133
|
+
## Save and load a model
|
|
134
|
+
|
|
135
|
+
Save a built `Sequential` model with built-in layers and activations.
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from depthml import Linear, Sequential, load_model, save_model
|
|
139
|
+
|
|
140
|
+
model = Sequential(Linear(1))
|
|
141
|
+
model.build((2,))
|
|
142
|
+
save_model("model.npz", model)
|
|
143
|
+
|
|
144
|
+
restored = load_model("model.npz").eval()
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`load_model()` builds the model from the file and restores its weights and buffers. It loads on CPU by default. Pass `device="cuda"` to load on CUDA.
|
|
148
|
+
|
|
149
|
+
## Checkpoints
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from depthml import AdamW, Linear, Sequential, StepLR, load_checkpoint, save_checkpoint
|
|
153
|
+
|
|
154
|
+
backbone, head = Linear(8), Linear(2)
|
|
155
|
+
model = Sequential(backbone, head)
|
|
156
|
+
model.build((4,))
|
|
157
|
+
optim = AdamW([
|
|
158
|
+
{"params": backbone.parameters(), "learning_rate": 1e-4},
|
|
159
|
+
{"params": head.parameters(), "weight_decay": 0.0},
|
|
160
|
+
], learning_rate=1e-3)
|
|
161
|
+
schedule = StepLR(optim, step_size=10)
|
|
162
|
+
|
|
163
|
+
save_checkpoint("checkpoint.npz", model, scheduler=schedule)
|
|
164
|
+
load_checkpoint("checkpoint.npz", model, scheduler=schedule)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Build the model before loading. Pass an optimizer to save its settings and buffers. `scheduler=` saves the scheduler and its optimizer. To resume training, use the same model, optimizer, and parameter groups.
|
|
168
|
+
Use `register_parameter(name, tensor)` for weights added after `build()`. Use `register_buffer(name, tensor)` for custom buffers.
|
|
169
|
+
|
|
170
|
+
## ONNX export
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
python -m pip install onnx onnxruntime
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Use the trained model from the [training example](#training).
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
import numpy as np
|
|
180
|
+
import onnxruntime as ort
|
|
181
|
+
from depthml import Tensor, export_onnx, no_grad
|
|
182
|
+
|
|
183
|
+
model.eval()
|
|
184
|
+
export_onnx("model.onnx", model)
|
|
185
|
+
|
|
186
|
+
x = np.array([[0., 1.], [1., 0.]], dtype=np.float32)
|
|
187
|
+
session = ort.InferenceSession("model.onnx", providers=["CPUExecutionProvider"])
|
|
188
|
+
actual, = session.run(None, {session.get_inputs()[0].name: x})
|
|
189
|
+
with no_grad():
|
|
190
|
+
expected = model(Tensor(x)).data
|
|
191
|
+
|
|
192
|
+
np.testing.assert_allclose(actual, expected, rtol=1e-5, atol=1e-6)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Export supports float32 `Sequential` models with `Linear`, `Conv2d`, `Flatten`, `MaxPooling2D`, `GlobalAveragePooling2D`, `Dropout`, `ReLU`, `LeakyReLU`, `ELU`, `Sigmoid`, `Tanh`, and `Softmax`. Batch size can vary, and the sample shape comes from `build()`.
|
|
196
|
+
|
|
197
|
+
## CPU benchmark
|
|
198
|
+
|
|
199
|
+
Training step on 64 samples with a 128 → 64 → 10 MLP, ReLU, MSE, and SGD. Median and range across six runs, in ms.
|
|
200
|
+
|
|
201
|
+
| DepthML | PyTorch | TensorFlow |
|
|
202
|
+
| ---: | ---: | ---: |
|
|
203
|
+
| 3.02 (1.37–3.94) | **0.64 (0.43–0.90)** | 13.42 (8.22–16.68) |
|
|
204
|
+
|
|
205
|
+
Intel Core i5-11400H, Windows 11, Python 3.11.15. DepthML 0.3.0, DepthTensor 3.0.0, PyTorch 2.12.1+cpu, TensorFlow 2.21.0.
|
|
206
|
+
|
|
207
|
+
```sh
|
|
208
|
+
python -m pip install torch tensorflow-cpu
|
|
209
|
+
python benchmarks/run.py
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Build
|
|
213
|
+
|
|
214
|
+
```sh
|
|
215
|
+
python -m pip install build pytest
|
|
216
|
+
python -m build
|
|
217
|
+
python -m pip install dist/depthml-0.3.0-py3-none-any.whl
|
|
218
|
+
python -m pytest -q
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
With PyTorch installed, run `python -m pytest -q tests/numerical/test_pytorch_comparison.py` to compare Transformer outputs, gradients, and an AdamW update.
|
|
222
|
+
|
|
223
|
+
## License
|
|
224
|
+
|
|
225
|
+
[MIT](LICENSE)
|