config

Modelopt’s pydantic BaseModel used for any type of configuration in algorithms and mode.

Functions

ModeloptField

A pydantic.Field that enforces setting a default value.

get_kwargs_for_create_model_with_rules

Generate the kwargs for pydantic.create_model to auto-generate a rule config class.

ModeloptConfig ModeloptBaseConfig

Bases: BaseModel

Our 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.

Show default config as JSON
Default config (JSON):

{}

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_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]

ModeloptConfig ModeloptBaseRule

Bases: ModeloptBaseConfig

Our base config class for rule-based config classes.

Rules are what governs the configuration for modifying dynamic module classes.

Show default config as JSON
Default config (JSON):

{}

classmethod customize_rule(rule, key)

Construct custom rule according to the provided key which is matched.

Parameters:
  • rule (Dict[str, Any] | None | Dict[str, Dict[str, Any] | 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

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] | None | Dict[str, Dict[str, Any] | None]) –

Return type:

Dict[str, Dict[str, Any] | None]

ModeloptConfig ModeloptBaseRuleConfig

Bases: ModeloptBaseConfig

Our 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.

Show default config as JSON
Default config (JSON):

{}

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_model to 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] | None | Dict[str, Dict[str, Any] | 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_model is explicitly called in the respective config file. So this function is a workaround to at least lower the burden of correctly calling create_model.