coffeetrain 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.
- coffeetrain-0.1.0/.github/workflows/ci.yml +37 -0
- coffeetrain-0.1.0/.github/workflows/publish.yml +65 -0
- coffeetrain-0.1.0/.python-version +1 -0
- coffeetrain-0.1.0/PKG-INFO +108 -0
- coffeetrain-0.1.0/README.md +77 -0
- coffeetrain-0.1.0/pyproject.toml +59 -0
- coffeetrain-0.1.0/src/coffeetrain/__init__.py +71 -0
- coffeetrain-0.1.0/src/coffeetrain/callback.py +120 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/__init__.py +33 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/batch_size_scheduler.py +170 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/checkpointing.py +184 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/comet.py +183 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/early_stopping.py +91 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/ema.py +101 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/history.py +108 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/lr_monitor.py +48 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/parameter_counter.py +43 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/progress.py +94 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/schedule_logger.py +93 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/speed_monitor.py +96 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/swa.py +126 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/torchmetrics_callback.py +144 -0
- coffeetrain-0.1.0/src/coffeetrain/callbacks/wandb.py +330 -0
- coffeetrain-0.1.0/src/coffeetrain/events.py +60 -0
- coffeetrain-0.1.0/src/coffeetrain/metrics/__init__.py +13 -0
- coffeetrain-0.1.0/src/coffeetrain/metrics/common.py +101 -0
- coffeetrain-0.1.0/src/coffeetrain/model.py +139 -0
- coffeetrain-0.1.0/src/coffeetrain/optimizers.py +259 -0
- coffeetrain-0.1.0/src/coffeetrain/schedulers.py +216 -0
- coffeetrain-0.1.0/src/coffeetrain/state.py +104 -0
- coffeetrain-0.1.0/src/coffeetrain/trainer.py +466 -0
- coffeetrain-0.1.0/tests/test_batch_size_scheduler.py +173 -0
- coffeetrain-0.1.0/tests/test_trainer_callbacks.py +179 -0
- coffeetrain-0.1.0/uv.lock +1432 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
- main
|
|
8
|
+
- master
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
lint-and-test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
fail-fast: false
|
|
15
|
+
matrix:
|
|
16
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Checkout
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Setup uv
|
|
23
|
+
uses: astral-sh/setup-uv@v6
|
|
24
|
+
|
|
25
|
+
- name: Set up Python
|
|
26
|
+
uses: actions/setup-python@v5
|
|
27
|
+
with:
|
|
28
|
+
python-version: ${{ matrix.python-version }}
|
|
29
|
+
|
|
30
|
+
- name: Install package and dev dependencies
|
|
31
|
+
run: uv sync --group dev
|
|
32
|
+
|
|
33
|
+
- name: Lint (ruff)
|
|
34
|
+
run: uv run ruff check .
|
|
35
|
+
|
|
36
|
+
- name: Test (pytest)
|
|
37
|
+
run: uv run pytest -q
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
pypi-publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write
|
|
13
|
+
contents: read
|
|
14
|
+
environment:
|
|
15
|
+
name: pypi
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- name: Checkout
|
|
19
|
+
uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Setup uv
|
|
22
|
+
uses: astral-sh/setup-uv@v6
|
|
23
|
+
|
|
24
|
+
- name: Set up Python
|
|
25
|
+
uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.12"
|
|
28
|
+
|
|
29
|
+
- name: Verify tag matches project version
|
|
30
|
+
run: |
|
|
31
|
+
python - <<'PY'
|
|
32
|
+
import os
|
|
33
|
+
import pathlib
|
|
34
|
+
import tomllib
|
|
35
|
+
|
|
36
|
+
ref_name = os.environ["GITHUB_REF_NAME"]
|
|
37
|
+
if not ref_name.startswith("v"):
|
|
38
|
+
raise SystemExit(f"Expected tag starting with 'v', got: {ref_name}")
|
|
39
|
+
|
|
40
|
+
tag_version = ref_name[1:]
|
|
41
|
+
pyproject_path = pathlib.Path("pyproject.toml")
|
|
42
|
+
data = tomllib.loads(pyproject_path.read_text(encoding="utf-8"))
|
|
43
|
+
project_version = data["project"]["version"]
|
|
44
|
+
|
|
45
|
+
if tag_version != project_version:
|
|
46
|
+
raise SystemExit(
|
|
47
|
+
f"Tag version '{tag_version}' does not match pyproject version '{project_version}'."
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
print(f"Tag version matches pyproject version: {project_version}")
|
|
51
|
+
PY
|
|
52
|
+
|
|
53
|
+
- name: Install build tooling
|
|
54
|
+
run: uv pip install --system build twine
|
|
55
|
+
|
|
56
|
+
- name: Build distributions
|
|
57
|
+
run: python -m build
|
|
58
|
+
|
|
59
|
+
- name: Verify distributions
|
|
60
|
+
run: twine check dist/*
|
|
61
|
+
|
|
62
|
+
- name: Publish to PyPI
|
|
63
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
64
|
+
with:
|
|
65
|
+
verbose: true
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: coffeetrain
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lightweight event-driven PyTorch trainer with composable callbacks
|
|
5
|
+
Project-URL: Repository, https://github.com/paul-english/coffeetrain
|
|
6
|
+
Author: Paul English
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
Keywords: callbacks,ml,pytorch,training
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Requires-Dist: pydantic>=2.0.0
|
|
21
|
+
Requires-Dist: torch
|
|
22
|
+
Requires-Dist: torchmetrics>=1.8.2
|
|
23
|
+
Requires-Dist: tqdm
|
|
24
|
+
Provides-Extra: comet
|
|
25
|
+
Requires-Dist: comet-ml; extra == 'comet'
|
|
26
|
+
Provides-Extra: optimi
|
|
27
|
+
Requires-Dist: torch-optimi; extra == 'optimi'
|
|
28
|
+
Provides-Extra: wandb
|
|
29
|
+
Requires-Dist: wandb>=0.18.0; extra == 'wandb'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# coffeetrain
|
|
33
|
+
|
|
34
|
+
Lightweight event-driven PyTorch trainer with composable callbacks. Inspired by [MosaicML Composer](https://github.com/mosaicml/composer) but implemented fewer external dependencies and allowing a newer PyTorch version (2.10 as of this point).
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- **Event-driven lifecycle**: `fit_start`, `epoch_start`, `batch_start`, `before_forward`, `after_forward`, `before_loss`, `after_loss`, `before_backward`, `after_backward`, `batch_end`, `eval_*`, etc.
|
|
39
|
+
- **Composable callbacks**: EMA, SWA, checkpointing, W&B, Comet, early stopping, batch size scheduling, and more.
|
|
40
|
+
- **TrainerModel protocol**: Simple interface (`forward`, `loss`) for model integration.
|
|
41
|
+
- **Accelerate support**: Distributed training via HuggingFace Accelerator.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install coffeetrain
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Optional extras:
|
|
50
|
+
```bash
|
|
51
|
+
pip install coffeetrain[wandb,comet,optimi]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick Start
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from coffeetrain import Trainer, CosineWarmupScheduler, HistoryCallback, BestModelCheckpointer
|
|
58
|
+
from coffeetrain import create_optimizer
|
|
59
|
+
from coffeetrain.optimizers import OptimizerConfig
|
|
60
|
+
|
|
61
|
+
model = MyModel()
|
|
62
|
+
optimizer = create_optimizer(model.parameters(), OptimizerConfig(name="adamw", lr=1e-4, weight_decay=0.01))
|
|
63
|
+
scheduler = CosineWarmupScheduler(optimizer, warmup_steps=100, total_steps=1000)
|
|
64
|
+
|
|
65
|
+
trainer = Trainer(
|
|
66
|
+
model=model,
|
|
67
|
+
train_dataloader=train_loader,
|
|
68
|
+
optimizers=optimizer,
|
|
69
|
+
schedulers=scheduler,
|
|
70
|
+
max_epochs=10,
|
|
71
|
+
callbacks=[
|
|
72
|
+
HistoryCallback(save_dir="output"),
|
|
73
|
+
BestModelCheckpointer(save_dir="output", metric_name="loss", mode="min"),
|
|
74
|
+
],
|
|
75
|
+
)
|
|
76
|
+
trainer.fit()
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Callbacks
|
|
80
|
+
|
|
81
|
+
| Callback | Description |
|
|
82
|
+
|----------|-------------|
|
|
83
|
+
| `BestModelCheckpointer` | Save best model by metric |
|
|
84
|
+
| `HistoryCallback` | Track and save training history to JSON |
|
|
85
|
+
| `EMACallback` | Exponential moving average of weights |
|
|
86
|
+
| `SWACallback` | Stochastic weight averaging |
|
|
87
|
+
| `EarlyStoppingCallback` | Stop when metric stops improving |
|
|
88
|
+
| `WandbCallback` | Log to Weights & Biases |
|
|
89
|
+
| `CometCallback` | Log to Comet.ml |
|
|
90
|
+
| `BatchSizeSchedulerCallback` | Batch size warmup |
|
|
91
|
+
| `ScheduleLoggerCallback` | Log LR schedule phase transitions |
|
|
92
|
+
| `ParameterCounter` | Print parameter counts at start |
|
|
93
|
+
| `SpeedMonitor` | Track samples/sec |
|
|
94
|
+
| `ProgressCallback` | Print epoch summaries |
|
|
95
|
+
| `LRMonitor` | Log learning rates |
|
|
96
|
+
| `TorchMetricsCallback` | Integrate torchmetrics |
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
Apache-2.0
|
|
101
|
+
|
|
102
|
+
## Tests
|
|
103
|
+
|
|
104
|
+
From repository root:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
uv run pytest packages/coffeetrain/tests -q
|
|
108
|
+
```
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# coffeetrain
|
|
2
|
+
|
|
3
|
+
Lightweight event-driven PyTorch trainer with composable callbacks. Inspired by [MosaicML Composer](https://github.com/mosaicml/composer) but implemented fewer external dependencies and allowing a newer PyTorch version (2.10 as of this point).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Event-driven lifecycle**: `fit_start`, `epoch_start`, `batch_start`, `before_forward`, `after_forward`, `before_loss`, `after_loss`, `before_backward`, `after_backward`, `batch_end`, `eval_*`, etc.
|
|
8
|
+
- **Composable callbacks**: EMA, SWA, checkpointing, W&B, Comet, early stopping, batch size scheduling, and more.
|
|
9
|
+
- **TrainerModel protocol**: Simple interface (`forward`, `loss`) for model integration.
|
|
10
|
+
- **Accelerate support**: Distributed training via HuggingFace Accelerator.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install coffeetrain
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Optional extras:
|
|
19
|
+
```bash
|
|
20
|
+
pip install coffeetrain[wandb,comet,optimi]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from coffeetrain import Trainer, CosineWarmupScheduler, HistoryCallback, BestModelCheckpointer
|
|
27
|
+
from coffeetrain import create_optimizer
|
|
28
|
+
from coffeetrain.optimizers import OptimizerConfig
|
|
29
|
+
|
|
30
|
+
model = MyModel()
|
|
31
|
+
optimizer = create_optimizer(model.parameters(), OptimizerConfig(name="adamw", lr=1e-4, weight_decay=0.01))
|
|
32
|
+
scheduler = CosineWarmupScheduler(optimizer, warmup_steps=100, total_steps=1000)
|
|
33
|
+
|
|
34
|
+
trainer = Trainer(
|
|
35
|
+
model=model,
|
|
36
|
+
train_dataloader=train_loader,
|
|
37
|
+
optimizers=optimizer,
|
|
38
|
+
schedulers=scheduler,
|
|
39
|
+
max_epochs=10,
|
|
40
|
+
callbacks=[
|
|
41
|
+
HistoryCallback(save_dir="output"),
|
|
42
|
+
BestModelCheckpointer(save_dir="output", metric_name="loss", mode="min"),
|
|
43
|
+
],
|
|
44
|
+
)
|
|
45
|
+
trainer.fit()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Callbacks
|
|
49
|
+
|
|
50
|
+
| Callback | Description |
|
|
51
|
+
|----------|-------------|
|
|
52
|
+
| `BestModelCheckpointer` | Save best model by metric |
|
|
53
|
+
| `HistoryCallback` | Track and save training history to JSON |
|
|
54
|
+
| `EMACallback` | Exponential moving average of weights |
|
|
55
|
+
| `SWACallback` | Stochastic weight averaging |
|
|
56
|
+
| `EarlyStoppingCallback` | Stop when metric stops improving |
|
|
57
|
+
| `WandbCallback` | Log to Weights & Biases |
|
|
58
|
+
| `CometCallback` | Log to Comet.ml |
|
|
59
|
+
| `BatchSizeSchedulerCallback` | Batch size warmup |
|
|
60
|
+
| `ScheduleLoggerCallback` | Log LR schedule phase transitions |
|
|
61
|
+
| `ParameterCounter` | Print parameter counts at start |
|
|
62
|
+
| `SpeedMonitor` | Track samples/sec |
|
|
63
|
+
| `ProgressCallback` | Print epoch summaries |
|
|
64
|
+
| `LRMonitor` | Log learning rates |
|
|
65
|
+
| `TorchMetricsCallback` | Integrate torchmetrics |
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
Apache-2.0
|
|
70
|
+
|
|
71
|
+
## Tests
|
|
72
|
+
|
|
73
|
+
From repository root:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
uv run pytest packages/coffeetrain/tests -q
|
|
77
|
+
```
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "coffeetrain"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Lightweight event-driven PyTorch trainer with composable callbacks"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
authors = [{name = "Paul English"}]
|
|
12
|
+
license = {text = "Apache-2.0"}
|
|
13
|
+
keywords = ["pytorch", "training", "callbacks", "ml"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"License :: OSI Approved :: Apache Software License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"torch",
|
|
28
|
+
"tqdm",
|
|
29
|
+
"torchmetrics>=1.8.2",
|
|
30
|
+
"pydantic>=2.0.0",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
wandb = ["wandb>=0.18.0"]
|
|
35
|
+
comet = ["comet-ml"]
|
|
36
|
+
optimi = ["torch-optimi"]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Repository = "https://github.com/paul-english/coffeetrain"
|
|
40
|
+
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
dev = [
|
|
43
|
+
"ruff>=0.13.0",
|
|
44
|
+
"pytest>=9.0.2",
|
|
45
|
+
"pytest-timeout>=2.4.0",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
packages = ["src/coffeetrain"]
|
|
50
|
+
|
|
51
|
+
[tool.ruff]
|
|
52
|
+
target-version = "py312"
|
|
53
|
+
|
|
54
|
+
[tool.ruff.lint]
|
|
55
|
+
select = ["E9", "F63", "F7", "F82"]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
testpaths = ["tests"]
|
|
59
|
+
addopts = "-q"
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Lightweight event-driven PyTorch trainer with composable callbacks."""
|
|
2
|
+
|
|
3
|
+
from coffeetrain.events import Event
|
|
4
|
+
from coffeetrain.state import State
|
|
5
|
+
from coffeetrain.callback import Callback
|
|
6
|
+
from coffeetrain.model import TrainerModel, TrainerModelMixin, ModelWrapper
|
|
7
|
+
from coffeetrain.trainer import Trainer
|
|
8
|
+
from coffeetrain.schedulers import CosineWarmupScheduler, LinearWarmupScheduler, WSDScheduler
|
|
9
|
+
from coffeetrain.optimizers import (
|
|
10
|
+
OptimizerConfig,
|
|
11
|
+
create_optimizer,
|
|
12
|
+
create_optimizer_with_param_groups,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
# Callbacks
|
|
16
|
+
from coffeetrain.callbacks import (
|
|
17
|
+
CometCallback,
|
|
18
|
+
EMACallback,
|
|
19
|
+
HistoryCallback,
|
|
20
|
+
BestModelCheckpointer,
|
|
21
|
+
LRMonitor,
|
|
22
|
+
ParameterCounter,
|
|
23
|
+
SpeedMonitor,
|
|
24
|
+
ProgressCallback,
|
|
25
|
+
TorchMetricsCallback,
|
|
26
|
+
WandbCallback,
|
|
27
|
+
BatchSizeSchedulerCallback,
|
|
28
|
+
SWACallback,
|
|
29
|
+
ScheduleLoggerCallback,
|
|
30
|
+
EarlyStoppingCallback,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Metrics
|
|
34
|
+
from coffeetrain.metrics import MeanLoss, Perplexity, SafeMulticlassAccuracy
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
# Core
|
|
38
|
+
"Event",
|
|
39
|
+
"State",
|
|
40
|
+
"Callback",
|
|
41
|
+
"TrainerModel",
|
|
42
|
+
"TrainerModelMixin",
|
|
43
|
+
"ModelWrapper",
|
|
44
|
+
"Trainer",
|
|
45
|
+
"CosineWarmupScheduler",
|
|
46
|
+
"LinearWarmupScheduler",
|
|
47
|
+
"WSDScheduler",
|
|
48
|
+
# Optimizers
|
|
49
|
+
"OptimizerConfig",
|
|
50
|
+
"create_optimizer",
|
|
51
|
+
"create_optimizer_with_param_groups",
|
|
52
|
+
# Callbacks
|
|
53
|
+
"CometCallback",
|
|
54
|
+
"EMACallback",
|
|
55
|
+
"HistoryCallback",
|
|
56
|
+
"BestModelCheckpointer",
|
|
57
|
+
"LRMonitor",
|
|
58
|
+
"ParameterCounter",
|
|
59
|
+
"SpeedMonitor",
|
|
60
|
+
"ProgressCallback",
|
|
61
|
+
"TorchMetricsCallback",
|
|
62
|
+
"WandbCallback",
|
|
63
|
+
"BatchSizeSchedulerCallback",
|
|
64
|
+
"SWACallback",
|
|
65
|
+
"ScheduleLoggerCallback",
|
|
66
|
+
"EarlyStoppingCallback",
|
|
67
|
+
# Metrics
|
|
68
|
+
"MeanLoss",
|
|
69
|
+
"Perplexity",
|
|
70
|
+
"SafeMulticlassAccuracy",
|
|
71
|
+
]
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""Callback base class for training hooks."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict
|
|
4
|
+
|
|
5
|
+
from coffeetrain.state import State
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Callback:
|
|
9
|
+
"""Base class for training callbacks.
|
|
10
|
+
|
|
11
|
+
Callbacks receive State at each event and can read/modify it.
|
|
12
|
+
Override the methods corresponding to events you want to handle.
|
|
13
|
+
|
|
14
|
+
All methods are no-ops by default, so subclasses only need to
|
|
15
|
+
implement the hooks they care about.
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
class PrintLossCallback(Callback):
|
|
19
|
+
def batch_end(self, state: State) -> None:
|
|
20
|
+
print(f"Loss: {state.loss.item():.4f}")
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
# Initialization
|
|
24
|
+
def init(self, state: State) -> None:
|
|
25
|
+
"""Called after Trainer construction, before fit().
|
|
26
|
+
|
|
27
|
+
Use for callback initialization that needs access to state.
|
|
28
|
+
"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
# Fit lifecycle
|
|
32
|
+
def fit_start(self, state: State) -> None:
|
|
33
|
+
"""Called at the start of fit(), before any training."""
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
def fit_end(self, state: State) -> None:
|
|
37
|
+
"""Called at the end of fit(), after all training."""
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
# Epoch lifecycle
|
|
41
|
+
def epoch_start(self, state: State) -> None:
|
|
42
|
+
"""Called at the start of each epoch."""
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
def epoch_end(self, state: State) -> None:
|
|
46
|
+
"""Called at the end of each epoch, before evaluation."""
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
# Training batch lifecycle
|
|
50
|
+
def batch_start(self, state: State) -> None:
|
|
51
|
+
"""Called at the start of each training batch."""
|
|
52
|
+
pass
|
|
53
|
+
|
|
54
|
+
def before_forward(self, state: State) -> None:
|
|
55
|
+
"""Called before model.forward()."""
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def after_forward(self, state: State) -> None:
|
|
59
|
+
"""Called after model.forward(), state.outputs is set."""
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
def before_loss(self, state: State) -> None:
|
|
63
|
+
"""Called before model.loss()."""
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
def after_loss(self, state: State) -> None:
|
|
67
|
+
"""Called after model.loss(), state.loss is set."""
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
def before_backward(self, state: State) -> None:
|
|
71
|
+
"""Called before loss.backward()."""
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
def after_backward(self, state: State) -> None:
|
|
75
|
+
"""Called after loss.backward(), before optimizer step."""
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
def batch_end(self, state: State) -> None:
|
|
79
|
+
"""Called after optimizer step and scheduler step."""
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
# Evaluation lifecycle
|
|
83
|
+
def eval_start(self, state: State) -> None:
|
|
84
|
+
"""Called at the start of evaluation."""
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
def eval_batch_start(self, state: State) -> None:
|
|
88
|
+
"""Called at the start of each eval batch."""
|
|
89
|
+
pass
|
|
90
|
+
|
|
91
|
+
def eval_before_forward(self, state: State) -> None:
|
|
92
|
+
"""Called before model.forward() during eval."""
|
|
93
|
+
pass
|
|
94
|
+
|
|
95
|
+
def eval_after_forward(self, state: State) -> None:
|
|
96
|
+
"""Called after model.forward() during eval."""
|
|
97
|
+
pass
|
|
98
|
+
|
|
99
|
+
def eval_batch_end(self, state: State) -> None:
|
|
100
|
+
"""Called at the end of each eval batch."""
|
|
101
|
+
pass
|
|
102
|
+
|
|
103
|
+
def eval_end(self, state: State) -> None:
|
|
104
|
+
"""Called at the end of evaluation."""
|
|
105
|
+
pass
|
|
106
|
+
|
|
107
|
+
# Checkpointing support
|
|
108
|
+
def state_dict(self) -> Dict[str, Any]:
|
|
109
|
+
"""Return callback state for checkpointing.
|
|
110
|
+
|
|
111
|
+
Override to save callback-specific state.
|
|
112
|
+
"""
|
|
113
|
+
return {}
|
|
114
|
+
|
|
115
|
+
def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
|
|
116
|
+
"""Load callback state from checkpoint.
|
|
117
|
+
|
|
118
|
+
Override to restore callback-specific state.
|
|
119
|
+
"""
|
|
120
|
+
pass
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Generic callback implementations for the trainer."""
|
|
2
|
+
|
|
3
|
+
from coffeetrain.callbacks.batch_size_scheduler import BatchSizeSchedulerCallback
|
|
4
|
+
from coffeetrain.callbacks.checkpointing import BestModelCheckpointer
|
|
5
|
+
from coffeetrain.callbacks.comet import CometCallback
|
|
6
|
+
from coffeetrain.callbacks.early_stopping import EarlyStoppingCallback
|
|
7
|
+
from coffeetrain.callbacks.ema import EMACallback
|
|
8
|
+
from coffeetrain.callbacks.swa import SWACallback
|
|
9
|
+
from coffeetrain.callbacks.history import HistoryCallback
|
|
10
|
+
from coffeetrain.callbacks.lr_monitor import LRMonitor
|
|
11
|
+
from coffeetrain.callbacks.parameter_counter import ParameterCounter
|
|
12
|
+
from coffeetrain.callbacks.progress import ProgressCallback
|
|
13
|
+
from coffeetrain.callbacks.speed_monitor import SpeedMonitor
|
|
14
|
+
from coffeetrain.callbacks.torchmetrics_callback import TorchMetricsCallback
|
|
15
|
+
from coffeetrain.callbacks.wandb import WandbCallback
|
|
16
|
+
from coffeetrain.callbacks.schedule_logger import ScheduleLoggerCallback
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"BatchSizeSchedulerCallback",
|
|
20
|
+
"BestModelCheckpointer",
|
|
21
|
+
"CometCallback",
|
|
22
|
+
"EarlyStoppingCallback",
|
|
23
|
+
"EMACallback",
|
|
24
|
+
"SWACallback",
|
|
25
|
+
"HistoryCallback",
|
|
26
|
+
"LRMonitor",
|
|
27
|
+
"ParameterCounter",
|
|
28
|
+
"ProgressCallback",
|
|
29
|
+
"ScheduleLoggerCallback",
|
|
30
|
+
"SpeedMonitor",
|
|
31
|
+
"TorchMetricsCallback",
|
|
32
|
+
"WandbCallback",
|
|
33
|
+
]
|