lightweight Python library that simplifies deep learning training loops by providing an elegant event-based iteration system
Project description
dloop
A lightweight Python library for simpler deep learning training loops.
What is it?
dloop is a small library that helps clean up those messy training loops without imposing a whole framework on you. It won't allow you to train a neural network in 4 lines of code, nor will it auto-distribute your training run to thousands of GPUS, and it certainly wont make your model perform better nor converge faster... but if you enjoy rolling your own training loops it may handle some inconveniences for you.
Lest jump straight into an example. This is how your training loop could look like with dloop:
from dloop import Event, Loop, LoopEvents
train_loop = Loop(
dataloader,
max_epochs=3, # specify ONE stopping condition: epochs, steps, or time (in seconds)
# max_steps=50_000, # alternative: stop after 50k steps
# max_seconds=3600, # alternative: stop after 1 hour of training
events={ # [1]
"ModelParametersUpdate": Event(every_n_steps=16),
"Logging": Event(every_n_steps=100),
"DecreaseLR": Event(at_step=10_000),
"HourlyCheckpoint": Event(every_n_seconds=3600),
"TenMinuteWarning": Event(at_time=50 * 60) # 50 minutes into training
}
)
for batch, batch_events in train_loop: # [2]
# <your-code> run forward and compute gradients </your-code>
if "ModelParametersUpdate" in batch_events: # [3]
# <your-code> update model parameter </your-code>
if "Logging" in batch_events: # [4]
# <your-code> log progress </your-code>
if "DecreaseLR" in batch_events: # [5]
# <your-code> decrease the learning rate </your-code>
if "HourlyCheckpoint" in batch_events: # [6]
# <your-code> save checkpoint every hour </your-code>
if "TenMinuteWarning" in batch_events: # [7]
# <your-code> log warning that training will end soon </your-code>
if LoopEvents.EPOCH_END in batch_events: # [8]
# <your-code> run validation? Checkpoint? </your-code>
- [1]: You define custom events by just giving them a name and specifying when they should be triggered. Currently, we support triggers
every_n_steps,at_step,every_n_seconds,at_time, or through an arbitrarycondition_function(loop_state). - [2]:
train_loopwill yield as many batches as you've specified, and then stop when appropriate, looping over the dataloader under the hood if necessary. - [3-8]:
batch_eventswill contain events that were triggered for this iteration, allowing to handle all iteration and error related logic with simple if statements:# 3and# 4are examples of checks that will evaluate to true every n steps. For example, sinceModelParametersUpdatetriggers every 16 steps, condition# 3will evaluate to true in steps 15, 31, 47 etc, allowing you to implement gradient accumulation.# 5shows how you can use the same kind of logic for one-time step-based events (for example decreasing your LR after 10k steps).# 6demonstrates time-based recurring events that trigger on a regular time interval (e.g., saving checkpoints every hour).# 7shows one-time time-based events that trigger at a specific time since training started.- Finally,
# 8shows usage of other pre-defined events (likeLoopEvents.EPOCH_ENDandLoopEvents.TRAINING_END) which are automatically added by dloop
Note how dloop is completely framework agnostic. All <your-code> blocks are completely up to you to write using your preferred framework.
Features
- Clean loop abstraction with state tracking
- Multiple iteration limit options (by epochs, steps, or time)
- Event-based system to replace conditional checks
- Step-based events: trigger on specific steps or every N steps
- Time-based events: trigger at specific times or every N seconds
- Custom condition events: trigger based on any logic
- Framework-agnostic (works with PyTorch, JAX, TensorFlow, MLX, etc.)
- Minimal dependencies (just Python standard library)
- Works with any iterable data source
Installation
pip install dloop
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dloop-0.1.0.tar.gz.
File metadata
- Download URL: dloop-0.1.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.13.2 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a48feaedc77589a983e5945e08ef9e128f14d54a130a340a8ecf7de76dd182f4
|
|
| MD5 |
277a5663211d0a98411ceda41d33742a
|
|
| BLAKE2b-256 |
cabdcdb6155eec3c39f07b4c7e8e970b707cec232d36b4bfc368a6209c80af66
|
File details
Details for the file dloop-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dloop-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.13.2 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9158bb4092329ba1f876dc4d53b8422f1b012f440c259272b341f6582df508ec
|
|
| MD5 |
1d127e82a5e8c188949fba6918c4cb5a
|
|
| BLAKE2b-256 |
90ebc10c18ed4d4f5121213fa0390e7d1f8e6ec18a88c6d99e11f0222532801c
|