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/*
Metadata
Release files for pydrantic 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydrantic-0.0.3.tar.gz | 20.5 kB | Without provenance | Details for pydrantic-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydrantic-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for pydrantic-0.0.3-py3-none-any.whl |
Total release size: 36.8 kB
Release files / pydrantic-0.0.3.tar.gz
| Download URL | pydrantic-0.0.3.tar.gz |
|---|---|
| Size | 20.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b47d3552f65923ac3fb9f6c36021037bdf9100ee4ec016b1b307d0ec682f4764
|
|
BLAKE2b-256 checksum How to use checksums |
8d71951ba1cd0f9ea5a1ae57da0771064b267797acd19fc950600c3ab04f16c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.11
|
Release files / pydrantic-0.0.3-py3-none-any.whl
| Download URL | pydrantic-0.0.3-py3-none-any.whl |
|---|---|
| Size | 16.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4e5027387773170fc158f49d7493979d2768cee9f1b9f2dd83c6893f423d91cf
|
|
BLAKE2b-256 checksum How to use checksums |
1212e7ac804e12151be96f07b0db8f8d6947e3b5d091a60ee03179001afb4491
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.11
|
Security for pydrantic 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.