A simple library that facilitates the use of Pydantic models for configuration
Project description
pydrantic
What is This?
pydrantic is a simple library that makes it easier to use Pydantic models as configs for machine learning experiments.
The library augments Pydantic models with features inspired by other configuration libraries like (e.g. Hydra). These include:
- Command-line overrides: Users can override config fields from the command line.
- Serialization: Users can serialize configs to YAML, pickle, or dill files.
- Variables: Users can define variables that can be used in the config.
- Launching sweeps: Users can launch sweeps from the command line.
- Object instantiation: Users can instantiate Pydantic models from the command line or from a file.
Why use Pydantic models as configs?
pydrantic is a fork of Jordan's awesome pydra library.
pydrantic inherits the same CLI syntax from pydra, but uses Pydantic models for config classes.
So, in some sense, pydrantic is just a more opinionated version of pydra.
The choice of Pydantic models for config classes encourages a strict separation between config and functionality. It also enables a few nice features like safer serialization, built-in type validation, and dependencies between fields in the config.
Installation
To install the latest release from PyPI:
pip install pydrantic
To install from source:
git clone https://github.com/seyuboglu/pydrantic.git
cd pydrantic
pip install -e .
Usage
The Basics
Define the schema for your config in a file called config.py.
In this example, we will define a TrainConfig class that specifies the schema of a configuration for a training run.
Note that we inherit from RunConfig, which is a convenience class that adds a few useful features to the base BaseConfig class.
# path/to/config.py
from pydrantic import RunConfig
class TrainConfig(RunConfig):
# (1) define the fields for your config
lr: float = 1e-3
epochs: int = 10
batch_size: int = 128
# (2) define a method that will be called when the config is run
def run(self):
...
Now in separate file, you can define a script that instantiates a config and runs it:
- Instantiate the config with the desired values.
- Launch the config with
pydrantic.main. By usingpydrantic.main, you can override the values of the config fields from the command line.
# path/to/script.py
from config import TrainConfig
# (1) instantiate the config
config = TrainConfig(
lr=1e-4,
epochs=20,
batch_size=256,
)
if __name__ == "__main__":
# (3) run the config
pydrantic.main(config)
You can run this script with:
python path/to/script.py
Or override the values of specific fields:
python path/to/script.py lr=1e-5
Nesting Configs
Just like Pydantic models, configs can be nested.
# path/to/config.py
from pydrantic import BaseConfig, RunConfig
class ModelConfig(BaseConfig):
num_layers: int = 1
hidden_dim: int = 1024
class TrainConfig(RunConfig):
model: ModelConfig
lr: float = 1e-3
epochs: int = 10
batch_size: int = 128
def run(self):
...
Now, in a separate script, you can instantiate the config and run it:
# path/to/script.py
from config import TrainConfig, ModelConfig
# (1) instantiate the config
config = TrainConfig(
model=ModelConfig(
num_layers=10,
hidden_dim=1024,
),
lr=1e-4,
epochs=20,
batch_size=256,
)
if __name__ == "__main__":
# (3) run the config
pydrantic.main(config)
You can set nested fields from the command line using dots:
python script.py model.num_layers=5
Launching Sweeps
You can sweep over different configurations by creating a list of configs in a script.
For example, assuming you have defined a TrainConfig class in another file, you can
run a sweep over learning rates by creating a file path/to/script.py with the following contents:
# path/to/script.py
from ... import TrainConfig
import numpy as np
configs = []
for lr in np.logspace(-4, -2, 10):
configs.append(
TrainConfig(learning_rate=lr)
)
if __name__ == "__main__":
pydrantic.main(configs)
To launch the runs in parallel using Ray, you can run the script with the -p flag:
python path/to/script.py -p
Object Configs
We also provide support for creating configs that
We also provide a few helper functions to save configs to YAML, pickle, or dill files. For example:
config = MyConfig()
config.to_yaml("conf.yaml")
config = MyConfig.from_yaml("conf.yaml")
if __name__ == "__main__":
main()
Dev
Running Tests
To run the repo's test suite, use:
python -m unittest discover tests
Releasing
First bump the version in setup.py and commit the changes.
python3 -m pip install --upgrade build
python3 -m build
python3 -m pip install --upgrade twine
python3 -m twine upload --repository pypi dist/*
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 pydrantic-0.0.3.tar.gz.
File metadata
- Download URL: pydrantic-0.0.3.tar.gz
- Upload date:
- Size: 20.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b47d3552f65923ac3fb9f6c36021037bdf9100ee4ec016b1b307d0ec682f4764
|
|
| MD5 |
ef64f4474ef7848734e74561568615e2
|
|
| BLAKE2b-256 |
8d71951ba1cd0f9ea5a1ae57da0771064b267797acd19fc950600c3ab04f16c6
|
File details
Details for the file pydrantic-0.0.3-py3-none-any.whl.
File metadata
- Download URL: pydrantic-0.0.3-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e5027387773170fc158f49d7493979d2768cee9f1b9f2dd83c6893f423d91cf
|
|
| MD5 |
d66b48af6779aa3ec6d718ddddac5850
|
|
| BLAKE2b-256 |
1212e7ac804e12151be96f07b0db8f8d6947e3b5d091a60ee03179001afb4491
|