Key Advantages of Config Classes
- Validation: By defining expected data types and constraints using Pydantic validators, Config classes significantly reduce the risk of common errors such as typos, incorrect data types, or missing fields.
- Improved Documentation: With the structured nature of Config classes, documentation will be automatically generated from docstrings present in the class.
- Enhanced Manageability: As models evolve to become more sophisticated, Config classes provide a clear and organized way to manage configuration parameters within the Python code.
Config Class Hierarchy
The Config classes follow a clear inheritance hierarchy:- BaseConfig: The root class that inherits from Pydantic’s BaseModel, providing core validation functionality
- ModelConfig: Inherits from BaseConfig, specifically for model configurations
- DataConfig: Inherits from BaseConfig, specifically for data processor configurations
BaseConfig
BaseConfig is a subclass of Pydantic’s BaseModel class with specific constraints. Config classes in Model Zoo are immutable by default, which means that once a config object is created, its attributes cannot be modified. This immutability helps ensure configuration stability during model execution. However, there are cases where you need to set some attributes based on other values during initialization. For these situations, the only place where attribute assignments are allowed is within thepost_init
method. For example:
field_validators
to change the value of a field:
ModelConfig
ModelConfig is a subclass of BaseConfig with the additional requirement that any config classes inheriting from it must specify aname
field. This field’s type must be defined as Literal["name_of_the_model"]
. The value of this name field is what the system uses to determine which model to initialize.
DataConfig
DataConfig is a subclass of BaseConfig with the additional requirement that any config classes inheriting from it must specify adata_processor
field. This field’s type must be defined as Literal["name_of_the_data_processor"]
, which ensures the field exactly matches the specific data processor identifier the system should initialize.
Validation Mechanisms
Config classes in Model Zoo provide robust validation capabilities through Pydantic’s validation system. There are two main approaches to validation:- Field-level validation
- Model-level validation
Field-Level Validation
Field validators check individual parameters without considering other fields. Use field-level validation when a parameter’s constraints are independent of other configuration values.Model-Level Validation
Use model validators when parameters have interdependencies or when validation requires checking multiple fields together.Model validators must always return self and should use mode=“after”.
Config Validation Tool
Model Zoo provides a CLI tool to validate your configuration files before running your model. This helps catch configuration errors early in development. To validate a configuration file using the CLI:Further Reading
- For detailed information about validation mechanisms, refer to the Pydantic validation documentation.
- For Model Zoo-specific implementations, review the examples in the Model Zoo repository.