config
Modelopt’s pydantic BaseModel used for any type of configuration in algorithms and mode.
Classes
Our config base class for mode configuration. |
|
Our base config class for rule-based config classes. |
|
Our config base class for mode configuration that are purely made from rules. |
Functions
A pydantic.Field that enforces setting a default value. |
|
Generate the kwargs for |
- class ModeloptBaseConfig
Bases:
BaseModel,MutableMappingOur config base class for mode configuration.
The base class extends the capabilities of pydantic’s BaseModel to provide additional methods and properties for easier access and manipulation of the configuration.
Inherits from
collections.abc.MutableMappingso instances satisfyisinstance(cfg, Mapping)/isinstance(cfg, MutableMapping)checks and pick up the mixin methods (pop,popitem,setdefault,clear). Schema fields are fixed, so__delitem__raisesTypeError; the inheritedpop/clear/popitemtherefore also raise on any existing key, whilepop(key, default)for a missing key still returns the default normally.- get(key, default=None)
Get the value for the given key (can be name or alias) or default if not found.
- Parameters:
key (str)
default (Any)
- Return type:
Any
- get_field_name_from_key(key)
Get the field name from the given key (can be name or alias of field).
- Parameters:
key (str)
- Return type:
str
- items()
Return the items of the config with keys as aliases if possible.
- Return type:
ItemsView[str, Any]
- keys()
Return the keys (aliases prioritized over names) of the config.
- Return type:
KeysView[str]
- model_config = {'extra': 'forbid', 'validate_assignment': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_dump(**kwargs)
Dump the config to a dictionary with aliases and no warnings by default.
- model_dump_json(**kwargs)
Dump the config to a json with aliases and no warnings by default.
- update(config)
Update the config with the given config dictionary.
- Parameters:
config (dict[str, Any])
- Return type:
None
- values()
Return the values of the config.
- Return type:
ValuesView[Any]
- class ModeloptBaseRule
Bases:
ModeloptBaseConfigOur base config class for rule-based config classes.
Rules are what governs the configuration for modifying dynamic module classes.
- classmethod customize_rule(rule, key)
Construct custom rule according to the provided key which is matched.
- Parameters:
rule (dict[str, Any] | dict[str, dict[str, Any] | None] | None)
key (str)
- Return type:
dict[str, Any] | None
- classmethod get_rule_type(wrapped_only=False)
Get the rule type for the given ModeloptBaseConfig.
- Parameters:
wrapped_only (bool)
- Return type:
TypeAlias
- model_config = {'extra': 'forbid', 'validate_assignment': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- classmethod validate_rule(rule)
Validate a rule with the current cls rule.
We will check the full rule type (wrapped and unwrapped) and then return the wrapped type.
- Parameters:
rule (dict[str, Any] | dict[str, dict[str, Any] | None] | None)
- Return type:
dict[str, dict[str, Any] | None]
- class ModeloptBaseRuleConfig
Bases:
ModeloptBaseConfigOur config base class for mode configuration that are purely made from rules.
The base class extends the capabilities of pydantic’s BaseModel to provide additional methods and properties for easier access and manipulation of the configuration.
- model_config = {'extra': 'allow', 'validate_assignment': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- classmethod register_default(extra_default)
Register a new default value for the given key.
- Parameters:
extra_default (dict[str, dict[str, dict[str, Any] | None]])
- Return type:
None
- classmethod unregister_default(key)
Unregister the default value for the given key.
- Parameters:
key (str)
- Return type:
None
- ModeloptField(default=PydanticUndefined, **kwargs)
A pydantic.Field that enforces setting a default value.
- Parameters:
default (Any)
- get_kwargs_for_create_model_with_rules(registry, default_rules, doc)
Generate the kwargs for
pydantic.create_modelto auto-generate a rule config class.- Parameters:
registry (Any) – The dynamic module registry that contains all relevant dynamic modules.
rule_fields – The fields that the rule-based config class should have.
doc (str) – The docstring for the rule-based config class.
default_rules (dict[str, dict[str, Any] | dict[str, dict[str, Any] | None] | None])
- Return type:
dict[str, Any]
A rule-based config class is a config class that purely consists of fields that pertain to rules. We can procedurally generate these rule config classes by using
from pydantic import create_model MyRuleConfigs = create_model( "MyRuleConfigs", **get_create_model_kwargs_for_rule_model(registry, rule_fields) )
For more info and example usage, you can take a look at
SparseMagnitudeConfig.Note
We have this convenience function in place since autodocs only get generated when
create_modelis explicitly called in the respective config file. So this function is a workaround to at least lower the burden of correctly callingcreate_model.