Skip to main content

Yet another config mixin/manager.

Project description

Python 3.10 License: Apache 2.0 test codecov PyPI

configmixin

An ultra lightweight configuration management library for machine learning inspired by the Hugging Face 🤗 diffusers library. Add automatic configuration handling to any class with a simple mixin pattern. Please refer to the documentation for more details.

Features

  • 🔗 Mixin Pattern: Add config management to models, trainers, or any class
  • 💾 Save/Load: Automatic JSON serialization with customizable serialization logic
  • Decorator Support: @register_to_config for automatic parameter registration
  • 🎯 Selective Exclusion: Control which parameters are saved to config

Installation

From PyPI:

pip install just-config-mixin

If you are interested in the experimental (i.e., unstable and undertested) version, you can install it from GitHub:

pip install git+https://github.com/Guest400123064/configmixin.git

Core Use Cases

1. PyTorch Model with Configuration

import torch.nn as nn
from configmixin import ConfigMixin, register_to_config


class TransformerModel(nn.Module, ConfigMixin):
    config_name = "model_config.json"

    @register_to_config
    def __init__(
        self,
        vocab_size: int = 30000,
        hidden_size: int = 768,
        num_layers: int = 12,
        dropout: float = 0.1
    ):
        super().__init__()
        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.dropout = dropout

        # Build model layers
        self.embedding = nn.Embedding(vocab_size, hidden_size)
        self.layers = nn.ModuleList([
            nn.TransformerEncoderLayer(hidden_size, 8, batch_first=True)
            for _ in range(num_layers)
        ])


model = TransformerModel(hidden_size=1024, num_layers=24)
print(model.config)

2. Configuration Save/Load

# Save configuration
model.save_config("./checkpoints/experiment_1")

# Load model with exact same configuration
loaded_model = TransformerModel.from_config(save_directory="./checkpoints/experiment_1")

# Or load from config dict
config_dict = {"vocab_size": 50000, "hidden_size": 512, "num_layers": 6, "dropout": 0.2}
model_from_dict = TransformerModel.from_config(config=config_dict)

3. Training Pipeline with Ignored Parameters

class ModelTrainer(ConfigMixin):
    config_name = "trainer_config.json"
    ignore_for_config = ["model", "optimizer"]  # Exclude runtime objects

    @register_to_config
    def __init__(
        self,
        learning_rate: float = 1e-4,
        batch_size: int = 32,
        num_epochs: int = 100,
        weight_decay: float = 0.01,
        # Runtime objects (ignored)
        model=None,
        optimizer=None,
        # Private params (auto-ignored due to underscore)
        _internal_state=None
    ):
        self.learning_rate = learning_rate
        self.batch_size = batch_size
        self.num_epochs = num_epochs
        self.weight_decay = weight_decay
        self.model = model
        self.optimizer = optimizer
        self._internal_state = _internal_state

# Only hyperparameters are saved, not runtime objects
trainer = ModelTrainer(
    learning_rate=2e-4,
    batch_size=64,
    model=some_model,
    optimizer=some_optimizer
)

trainer.save_config("./experiments/run_001")

# Config only contains: learning_rate, batch_size, num_epochs, weight_decay
# and the runtime objects are passed via `runtime_kwargs` in `from_config()`
trainer = ModelTrainer.from_config(save_directory="./experiments/run_001", runtime_kwargs={"model": <some_model>})

Complete Workflow

# Create components with configurations
model = TransformerModel(hidden_size=1024, num_layers=24)
trainer = ModelTrainer(learning_rate=1e-4, batch_size=64)

# Save all configurations to experiment directory
experiment_dir = "./experiments/run_001"
model.save_config(experiment_dir)
trainer.save_config(experiment_dir)

# Later: reproduce exact setup
loaded_model = TransformerModel.from_config(save_directory=experiment_dir)
loaded_trainer = ModelTrainer.from_config(save_directory=experiment_dir, runtime_kwargs={"model": loaded_model})

Why configmixin?

Perfect for ML workflows where you need:

  • Reproducible experiments with exact parameter tracking
  • Easy hyperparameter management built into your classes
  • Clean separation between config and runtime state

Contributing

Contributions welcome! Please submit a Pull Request.

License

Apache License 2.0 - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

just_config_mixin-0.2.0a1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

just_config_mixin-0.2.0a1-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file just_config_mixin-0.2.0a1.tar.gz.

File metadata

  • Download URL: just_config_mixin-0.2.0a1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for just_config_mixin-0.2.0a1.tar.gz
Algorithm Hash digest
SHA256 27e33b599ea0bc3fdffec624487a6f5e5bb1fa7cb8d05b670eb066f34faa33e5
MD5 bdd797ba1dd6e18033a406f72b563430
BLAKE2b-256 b86d65955769bfe51bce5ab694307227b8421bf17c78904e3c47921e4e729d24

See more details on using hashes here.

File details

Details for the file just_config_mixin-0.2.0a1-py3-none-any.whl.

File metadata

  • Download URL: just_config_mixin-0.2.0a1-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for just_config_mixin-0.2.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 235f548afa76e83afc7af38eeca6cd2848142097348bf206a36ef94473969167
MD5 993ad4ccc34570a34ac62c80198a8e68
BLAKE2b-256 1aa73db488996543fb377808d19c064e67b53688ee00d46563f6d17321038f74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page